JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Software Design » Effective Java  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 [Effective Java Reloaded] Static Factory和建構子的瓶頸
koji

秒速5センチメートル

站長

發文: 8420
積分: 19
於 2006-08-16 23:56 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
本文翻譯自JavaOne的Effective Java Reloaded,可以到JavaOne網頁看投影片

當要產生一個物件時,如果這個物件有超多的屬性時

1
2
3
4
new NutritionFacts(
  String name, int servingSize, int servingsPerCntnr,
  int totalFat, int saturatedFat, int transFat,
  int cholesterol, 15 more optional params!);


有時候產生一個物件時不需要這麼多參數,
但是難不成得提供2^n個建構子?

如果透過setter,遇到想產生immutable類別的物件時又不行

這時候就是靠Builder pattern了!!!

1.Builder的建構子只要"必須"的參數
2.其餘的屬性透過setter, 此setter回傳builder本身以達到chaining的效果

結果類似
1
2
NutritionFacts twoLiterDietCoke =
  new NutritionFacts.Builder("Diet Coke", 240, 8).sodium(1).build();


所以寫一個類別會如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class NutritionFacts {
  public static class Builder {
    public Builder(String name, int servingSize, int ervingsPerContainer){
    }
 
    public Builder totalFat(int val) {...
    }
 
    public Builder saturatedFat(int val) {...
    }
 
    public Builder transFat(int val) {...
    }
 
    public Builder cholesterol(int val) {...
    }
 
    ...// 15 more setters
    public NutritionFacts build() {
      return new NutritionFacts(this);
    }
  }
 
  private NutritionFacts(Builder builder) {
  }
}


koji edited on 2007-03-29 01:50
reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:[Effective Java Reloaded] Static Factory和建構子的瓶頸 [Re:koji]
ingramchen

Web monkey



發文: 479
積分: 12
於 2006-08-17 00:13 user profilesend a private message to usersend email to ingramchenreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
inner class Builder 應該是 static 的吧?

reply to postreply to post
MyBlog | 訂便當管理系統

作者 Re:[Effective Java Reloaded] Static Factory和建構子的瓶頸 [Re:koji]
koji

秒速5センチメートル

站長

發文: 8420
積分: 19
於 2006-08-17 00:31 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
拍謝拍謝沒貼正確orz..

koji


reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:[Effective Java Reloaded] Static Factory和建構子的瓶頸 [Re:koji]
ulu





發文: 27
積分: 0
於 2006-08-19 22:38 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
如果透過setter,遇到想產生immutable類別的物件時又不行

抱歉,可否解釋一下這句是怎麼做的?我覺得透過Builder的setter還是可以去更改NutritionFacts的屬性啊?


reply to postreply to post
作者 Re:[Effective Java Reloaded] Static Factory和建構子的瓶頸 [Re:ulu]
ingramchen

Web monkey



發文: 479
積分: 12
於 2006-08-19 23:08 user profilesend a private message to usersend email to ingramchenreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
ulu wrote:
如果透過setter,遇到想產生immutable類別的物件時又不行

抱歉,可否解釋一下這句是怎麼做的?我覺得透過Builder的setter還是可以去更改NutritionFacts的屬性啊?

Builder 的 "setter" (其實不是 setter) 只會更改 Builder 的內容, NutritionFacts 這個物件直到 Builder.build() 執行後才會產生,所以它可以是 immutable 的。


reply to postreply to post
MyBlog | 訂便當管理系統

作者 Re:[Effective Java Reloaded] Static Factory和建構子的瓶頸 [Re:koji]
psychokiller





發文: 581
積分: 1
於 2006-11-14 14:31 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我有時候會這樣
用 一個class D都是public field
public class D{

public String name;.....
}

然後就可以
NutritionFacts nutirionFacts= new NutritionFacts(D);


reply to postreply to post
» JWorld@TW »  Software Design » Effective Java

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8