《Effictive Java》Item 1: Consider static factory methods instead of constructors

advantage:
1st. unlike constructors, they have names.

2nd. unlike constructors,they are not required to create a new object each time they’re invoked.

3rd. unlike constructors,they can return an object of any subtype of their return type.
提供了靈活性,可以隱藏實現細節。可以根據不同的參數採用不同的實現....

4th. that they reduce the verbosity of creating parameterized type instances.


disadvantage:
1st.providing only static factory methods is that
classes without public or protected constructors cannot be subclassed.

2nd.they are not readily distinguishable from other static methods.
解決辦法:1、註釋 2、儘量採用通用的命名,比如ValueOf、of、getInstance、newInstance、get[i]Type[/i]、new[i]Type[/i]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章