int officialShopId = shopId; group.setShopId(officialShopId); XmpUser xmpSeller = buildXmpUser(seller.getSellerInfo()); group.setSeller(xmpSeller); List<XmpItem> xmpItems = buildXmpItemList(sellerList,clientName, officialShopId); group.setItems(xmpItems);
以上代码看上去比较杂乱无章,可以做一个简单分组。
int officialShopId = shopId; XmpUser xmpSeller = buildXmpUser(seller.getSellerInfo()); List<XmpItem> xmpItems = buildXmpItemList(sellerList,clientName, officialShopId); group.setShopId(officialShopId); group.setSeller(xmpSeller); group.setItems(xmpItems);
这样就清晰很多,一方面可读性变好了,另一方面会便于进一步的重构,比如抽取小方法。
关于对象设置,C#的语法明显比java简洁多了。
group = new Group() { ShopId = officialShopId, Seller = xmpSeller, Items = xmpItems };
java下面可能用fluent API是种选择。
group.setShopId(officialShopId)
.setSeller(xmpSeller)
.setItems(xmpItems);