《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 Type、new Type
    
   

   

你可能感兴趣的:(java)