}
第2步:  创建⼀个服务客户信息的资源类。该类充当演⽰中的服务器。客户详细信息在哪⼉?
public class CustomerResource {
private List < CustomerDto > customers;
/**
param name* @param customers initialize resource with existing customers. Act as database.
*/public CustomerResource(List < CustomerDto > customers) {
this.customers = customers;
}/**
* @return : all customers in list.
*/public List < CustomerDto > getAllCustomers() {
return customers;
}/**
* @param customer save new customer to list.
*/public void save(CustomerDto customer) {
customers.add(customer);
}/**
* @param customerId delete customer with id {@code customerId}
*/public void delete(String customerId) {
}
}
第3步: CustomerClientApp类是客户详细信息使⽤者,即客户端向服务器请求客户详细信息。CustomerResource充当服务器来提供客户信息。
customerdto是⽤于共享客户信息的数据传输对象。
public class CustomerClientApp {
private static final Logger LOGGER = Logger(CustomerClientApp.class); /**
* Method as act client and request to server for details.
*
* @param args program argument.
*/public static void main(String[] args) {
List < CustomerDto > customers = new ArrayList < > ();
CustomerDto customerOne = new CustomerDto("1","Kelly","Brown");
CustomerDto customerTwo = new CustomerDto("2","Alfonso","Bass"); customers.add(customerOne);
customers.add(customerTwo);
CustomerResource customerResource = new CustomerResource(customers); LOGGER.info("All customers:-");
List < CustomerDto > allCustomers = AllCustomers(); printCustomerDetails(allCustomers);
LOGGER.info("----------------------------------------------------------");
LOGGER.info("Deleting customer with id {1}");
customerResource.Id());
allCustomers = AllCustomers();
printCustomerDetails(allCustomers);
LOGGER.info("----------------------------------------------------------");
LOGGER.info("Adding customer three}");
CustomerDto customerThree = new CustomerDto("3","Lynda","Blair"); customerResource.save(customerThree);
allCustomers = AllCustomers();
printCustomerDetails(allCustomers);
}
private static void printCustomerDetails(List < CustomerDto > allCustomers) { allCustomers.forEach(customer - > LOGGER.FirstName())); }
}
结果
减少⽹络流量
简化远程对象和远程接⼝
在更少的远程调⽤中传输更多数据
减少代码重复
引⼊陈旧的转移对象
由于同步和版本控制⽽增加了复杂性。
适⽤场景
客户要求提供多种信息。⽽且信息是相关的。
当你想提⾼性能以获取资源时。
您希望减少远程呼叫的数量。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。