第1條:考慮用靜態(tài)工廠方法替代構(gòu)造器

Item 1:Consider static factory methods instead of constructors

Advantages:

? ? 1.One advantage of static factory methods is that,unlike constructors,they have names.

? ? ? ?構(gòu)造器只能以類名命名,而靜態(tài)工廠方法可以有自己的名字??紤]如下兩種情況:

? ? ? ? a.使用BigInteger(int, int, Random)構(gòu)建一個素數(shù)

? ? ? ? b.構(gòu)造器的參數(shù)類型相同但是順序不同

? ? ? ? ?構(gòu)造方法名稱只能是類名,此時是很難知道構(gòu)造器要表達的意義以及各個構(gòu)造器之間的區(qū)別,容易導(dǎo)致程序員誤調(diào)。當使用靜態(tài)工廠方法,就沒有這些弊端,因為可以通過方法名稱加以說明。

? ? 2.A second advantage of static factory methods is that, unlike constructors,they are not required to create a new object each time they`re invoked.

????????通過靜態(tài)工廠方法可以很好的控制類的實例,它可以實現(xiàn)實例的緩存、單例、不可實例化、實例不可變等功能。

????3.A third advantage of static factory methods is that ,unlike constructors,they can return an object of any subtype of their return type.

????????可以返回其返回值類型的任何不同的子類并且強迫客戶端面向接口編程.

? ? 4.A fourth advantage of static factory methods is that the class of returned object can vary from call to call as a function of the input parameters.

? ? ? ? 可以根據(jù)參數(shù)返回不同的實現(xiàn),類似策略模式。

? ? 5.A fifth advantage of static factory motheds is that? the class of returned object need not exist when then class containing the methed is written.

? ? ? ? 靜態(tài)方法返回的類,在編寫該靜態(tài)方法的時候可以并不存在。

Shortcoming:

1.The main limitation of providing only static factory method is that classes without public or protected constructors cant not be subclassed.

????只提供靜態(tài)工廠方法,但是沒有public或者protected的構(gòu)造器,則類不能被繼承。

2.A second shortcoming of static factory mothed is that they of hard for programmers to find.

? ? 不是所有人都習(xí)慣使用靜態(tài)工廠方法創(chuàng)建對象。用靜態(tài)工廠方法代替構(gòu)造器,會使程序員花費一定時間去了解如何去實例化相應(yīng)的對象。

常用靜態(tài)工廠方法的命名方式:

????? from—A type-conversion method that takes a single parameter and returns a

????????corresponding instance of this type, for example:

????????Date d = Date.from(instant);

????? of—An aggregation method that takes multiple parameters and returns an instance of this type that incorporates them, for example:

????????Set faceCards = EnumSet.of(JACK, QUEEN, KING);

????? valueOf—A more verbose alternative to from and of, for example:

????????BigInteger prime = BigInteger.valueOf(Integer.MAX_VALUE);

????? instance or getInstance—Returns an instance that is described by its parameters (if any) but cannot be said to have the same value, for example:

????????StackWalker luke = StackWalker.getInstance(options);

????? create or newInstance—Like instance or getInstance, except that the

????????method guarantees that each call returns a new instance, for example:

????????Object newArray = Array.newInstance(classObject, arrayLen);

????? getType—Like getInstance, but used if the factory method is in a different class.? Type is the type of object returned by the factory method, ????for example:

????????FileStore fs = Files.getFileStore(path);

????? newType—Like newInstance, but used if the factory method is in a different class.Type is the type of object returned by the factory method, for example:

????????BufferedReader br = Files.newBufferedReader(path);

????? type—A concise alternative to getType and newType, for example:

????????List litany = Collections.list(legacyLitany);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容