JAVA填充POJO void方法vs返回字段的方法哪个更好?

时间:2015-12-23 15:57:04

标签: java java-ee memory-management coding-style pojo

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;
}

VS

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));

1 个答案:

答案 0 :(得分:0)

我建议您查看Apache BeanUtils.copyProperties

public static void copyProperties(Object dest, Object orig)
           throws IllegalAccessException, InvocationTargetException

虽然可能会对类型安全和性能造成不利影响,但它可能会更快地完成您想要的操作。

链接:BeanUtils