JAVA 1.8
从另一个类“人”填充“客户”类的最具可读性/效率的方法是什么?
AddressInfo,ContactPreferences 和 AuthorizationRoles 都是包含多个字段的复杂POJOS,我们需要进行额外的数据库调用和服务调用。
public Customer getCustomerFromPerson(Person per) {
Customer cust = new Customer();
populateAddressInfo(cust, per);
populateContactPreferences(cust, per);
populateAuthorizationRoles(cust, per);
...
return cust;
}
public Customer getCustomerFromPerson(Person per) {
Customer cust = new Customer();
cust.setAddressInfo(getAddressInfoFromPerson(per));
cust.setContactInfo(getContactPreferencesForCust(cust, per));
cust.setAuthRoles(getAuthorizationRoles(per));
...
return cust;
}
Customer.AddressInfo!= Person.AddressInfo。它们的映射非常少,我们从DB / Soap中提取其他数据以获得完整的Customer.Addressinfo cust.getAddressInfo.setIsBillingAddress(的 Webservicecall(per.getAddressInfo));
答案 0 :(得分:0)
我建议您查看Apache BeanUtils.copyProperties
。
public static void copyProperties(Object dest, Object orig)
throws IllegalAccessException, InvocationTargetException
虽然可能会对类型安全和性能造成不利影响,但它可能会更快地完成您想要的操作。
链接:BeanUtils