`
pcpig
  • 浏览: 87375 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

《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、getType、newType
    
   

   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics