Item 2: Consider a builder when faced many constructor parameters.
當(dāng)類(lèi)的構(gòu)造參數(shù)越來(lái)越多我們?cè)撛趺崔k?
? ? a.重疊構(gòu)造器模式(如下 effective java 中的例子)

????????優(yōu)點(diǎn):感覺(jué)沒(méi)啥優(yōu)點(diǎn),參數(shù)少時(shí),可以用用。
? ? ? ? 缺點(diǎn): 客戶(hù)端調(diào)用構(gòu)造器很麻煩,需要程序員知道參數(shù)個(gè)數(shù)、參數(shù)順序、參數(shù)意義,很容易調(diào)用錯(cuò)誤,同時(shí)會(huì)導(dǎo)致代碼不可讀。
? ? ? ? b.JavaBean 方式創(chuàng)建, 通過(guò)setter方法設(shè)置參數(shù)

? ? ? ? ? ? 優(yōu)點(diǎn):構(gòu)造過(guò)程可讀性變強(qiáng)
? ? ? ? ? ? 缺點(diǎn):構(gòu)造過(guò)程被拆分,無(wú)法保證構(gòu)造過(guò)程中JavaBean的一致性;JavaBean 方式導(dǎo)致類(lèi)不再是不可變的了;要求程序員去保證線程安全。
? ? ? ? ? ? c.構(gòu)造器模式


????????????????優(yōu)點(diǎn): 構(gòu)建器更加的靈活,方便調(diào)用、可重復(fù)調(diào)用以生成多個(gè)對(duì)象、代碼可讀性強(qiáng)
? ? ? ? ? ? ? ? 缺點(diǎn):有一定的性能消耗,當(dāng)性能要求很高的情況下,需慎重使用。
? ? In summary, the Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters.
? ? 當(dāng)構(gòu)造函數(shù)或者靜態(tài)工廠方法參數(shù)越來(lái)越多的情況下,構(gòu)建器是一個(gè)不錯(cuò)的選擇。