int officialShopId = shopId; group.setShopId(officialShopId); XmpUser xmpSeller = buildXmpUser(seller.getSellerInfo()); group.setSeller(xmpSeller); List<XmpItem> xmpItems = buildXmpItemList(sellerList,clientName, officialShopId); group.setItems(xmpItems);
以上代碼看上去比較雜亂無章,能夠作一個簡單分組。java
int officialShopId = shopId; XmpUser xmpSeller = buildXmpUser(seller.getSellerInfo()); List<XmpItem> xmpItems = buildXmpItemList(sellerList,clientName, officialShopId); group.setShopId(officialShopId); group.setSeller(xmpSeller); group.setItems(xmpItems);
這樣就清晰不少,一方面可讀性變好了,另外一方面會便於進一步的重構,好比抽取小方法。ui
關於對象設置,C#的語法明顯比java簡潔多了。spa
group = new Group() { ShopId = officialShopId, Seller = xmpSeller, Items = xmpItems };
java下面可能用fluent API是種選擇。code
group.setShopId(officialShopId)
.setSeller(xmpSeller)
.setItems(xmpItems);