Improve java, consider static factory method instead of constructor

  Static factory method instead of constructor
  
  Speaking of this, a lot can be said. After working on the project for more than a year, I gradually felt it.
  
  Speaking of constructors,
  
  everyone knows that constructors allow us to obtain an instance of ourselves or others where and where. We are unscrupulous in using new but have never considered how others feel. In fact, new, new an object is to open up a memory space for this object. If it is new everywhere, everywhere, all over the mountains ...
  
  colorful new, the essence is the same One
  
  sentence: Although the constructor is almighty, but it must be cherished.
  
  Let's talk about the static factory method
  
  static factory method, as the name suggests, is just a static method that returns an instance of the class. There is an inappropriate analogy here. I think the benefits of static factory methods, like hand taps, always have water (objects). Just use this method.
  
  Take a look at the following simple example:
  
  public static Boolean valueOfBoolean (Boolean b)
  
  {
  
  return b? Boolean.TRUE: Boolean.FALSE;
  
  }
  
  #boolean basic type is converted to Boolean object reference.
  
  #staticAccording
  
  to the above example, compared with the constructor, let's discuss with me:
  
  1 It starts to have names
  
  2 You don't have to call them every time, create a new object
  
  3 Return to any sub-type object
  
  service access API of the original return type- --- Flexible dynamic factory
  
  The following implements a service provider interface and a default provider:
  
  package src.day01;
  
  import java.util.concurrent.ConcurrentHashMap;
  
  /**
  
  * Created with IntelliJ IDEA.
  
  * User: Li
  
  * Date: 14-5-12
  
  * Time: 下午10:52
  
  * To change this template use File | Settings | File Templates.
  
  */
  
  public interface Service {
  
  }
  
  public interface Provider {
  
  Service newService();
  
  }
  
  public class Services {
  
  private Services(){}
  
  private static final Map<String , Provider> providers = new ConcurrentHashMap<String , Provider>();
  
  public static final String DEFAULT_PROVIDER_NAME = "<def>";
  
  public static void registerDeafaultProvider(Provider p)
  
  {
  
  registerProvider (DEFAULT_PROVIDER_NAME, p);
  
  }
  
  public static void registerProvider (String name, Provider p)
  
  {
  
  providers.put (name, p);
  
  }
  
  public static Service new Instance ()
  
  {
  
  return newInstance (DEFAULT_PROVIDER_NAME);
  
  }
  
  public static Service newInstance ( String name)
  
  {
  
  Provider p = providers.get (name);
  
  if (p == null)
  
  throw new IllegalArgumentException (
  
  "no ..."
  
  );
  
  return p.newService ();
  
  }
  
  }
  
  #providers
  
  # newInstance This method is static The factory method
  
  this example tells us that when
  
  they create a parameterized type instance, the code is more concise.
  
  Summary
  
  Static Factory Method
  
  1 It started to have a name
  
  2 You don't have to call them every time to create a new object.
  
  3 Return objects of any subtype of the original return type.
  
  4 When they create a parameterized type instance, the code is more concise.
  
  (Life is inseparable from finding objects, looking for ways, what do you say?)
  
  Thanks and resource sharing
  
  walked step by step on the road, I hope everyone will join me.

Guess you like

Origin www.cnblogs.com/zhenhua1618/p/12729931.html