流利的对象创建
關于此主題的文章很多(絕大多數),但我只是想貢獻我的兩分錢,寫一篇簡短的文章,介紹如何使用Java中的Fluent Object Creation模式或對象構建器實例化Value Objects。
值對象是由其狀態(值)而不是其在內存中的地址定義的抽象。 價值對象的示例是諸如金錢,數字,坐標等之類的東西。它們不是用來描述業務對象,而是用于描述具體不可分割的實體。 此外,它們是將其添加到收藏和地圖的絕佳選擇。
在Java中, Value Objects應該聲明為final,并且不提供setter方法,基本上使它在創建后保持不變,這是非常重要的
需求。 最終聲明它們會使它們無法充當父對象。 這是通過設計完成的,因為價值對象應該對小的具體實體進行建模。 原因是我們應該能夠創建和比較這些對象的多個副本,這總是由狀態而不是通過引用來完成。 另外,您應該聲明適當的equals()和hashCode()方法以符合適當的值對象的條件。 在C ++中,適用相同的原理。 在C ++中,您應該使用Copy構造函數并重載賦值和比較運算符。 流利的對象創建模式使價值對象實例化優雅而干凈。 很快就會看到,使用流暢的對象創建可以帶來很多好處。
從API用戶的角度來看,應用此模式的最終結果將如下所示:
Money fiveEuros = new Money.Builder().currency(Currency.EURO).value(5.0L).countryOfOrigin("Spain").type("Coin").reverse("Map of Europe").obverse("Map of Spain").addColor("Bronze").addColor("Silver").year("1880") .build();我認為您會同意,這種模式與之相反,流程更加順暢:
Money fiveEuros = new Money(); fiveEuros.setCurrency(Currency.EURO); fiveEuros.setValue(5.0L); fiveEuros.countryOfOrigin("Spain"); fiveEuros.type("Coin"); fiveEuros.reverse("Map of Europe"); fiveEuros.obverse("Map of Spain");List<String> colors = new ArrayList<String>(); for(String color: new String[] {"Bronze", "Silver"}) {colors.add(color); }fiveEuros.setColors(colors); fiveEuros.setYear("1880");這似乎壞了,并且有很多打字和重復。 在我看來,這是一個構建非常大的價值對象的示例,其中大多數往往很小。 在討論以這種方式創建對象的好處之前,讓我們看一下這種模式的結構:
public final class Money {private Long value;private String countryOfOrigin; private Currency currency;private String type; private String reverse;private String obverse; private List<String> colors;private Date year; private Money() { }// -- getters, hashCode, equals -- //// Static inner Builder classpublic static class Builder {private Money _temp = new Money(); public Builder value(Long val) { _temp.value = val; return this; } public Builder countryOfOrigin(String countryOfOrigin) { _temp.contryOfOrigin = countryOfOrigin;return this; } public Builder currency(Currency c) { _temp.currency = c; return this; } public Builder type(String t) { _temp.type = t; return this; } public Builder reverse(String r) { _temp.reverse = r; return this; }public Builder obverse(String o) { _temp.obverse = o; return this; }public Builder addColor(String c) { if(_temp.colors == null) {_temp.colors = new ArrayList<String>(); }_temp.colors.add(c); return this; }public Builder year(String y) {if(y == null || y.isEmpty()) {_temp.year = new Date();}else {_temp.year = DateFormat.parse(y);} return this; } public Money build() {// Validate objectif(Strings.isNullOrEmpty(_temp.name) || _temp.currency == null) {throw new IllegalArgumentException("Coin currency and value required");} return _temp; }} } 這也是一個問題,但是我更喜歡靜態內部類方法。 我喜歡將構建者稱為“規范性”
Money.Builder。 還需要將其設為靜態 ,因為構建器實例需要獨立于封閉類生活。 我喜歡這種模式,因為它具有以下優點:
結論
使用流暢的創建模式需要更多的工作,但是最終獲得回報的好處。 它使實例化對象非常優雅和干凈。 您不必將其與Value Objects一起使用,使用Fluent Object Creation的大多數好處是當您需要構建相當復雜的對象圖時,但是我想證明它也可以適用 小價值的對象。
參考:我們的JCG合作伙伴 Luis Atencio在Reflective Thought博客上的Fluent Object Creation 。
翻譯自: https://www.javacodegeeks.com/2013/01/fluent-object-creation.html
總結
- 上一篇: 安卓四清是哪四清(安卓四清)
- 下一篇: Google Guava多集