Factory mode explaining

Source: Internet
Author: User

Factory mode is a form of creation that provides a way to create objects that solves the problem between the user and the product, hides the implementation details of the production product, and the user only needs to be concerned with the final result. The official definition is: Define an interface to create an object, let its subclasses decide which factory class to instantiate, and the factory pattern to defer its creation to subclasses.

Here are some examples of how to use the poor lifting method:

1. Users go to the car factory to buy a car, need what car to buy what car, not as long as the car production details.

2. Hibernate inside Configure the database to select a dialect, configure what method will appear what database, hide the implementation details.

3. We go to the hour to buy milk, choose what brand to buy what brand, do not know how to produce milk.

Simple Factory
 Package pattern.factory;  Public Interface Milk {    public  String getName ();}
 package   pattern.factory;  public  class  MengNiu implements   Milk {@Override  public   String GetName () { return " Mengniu "; }}
 package   pattern.factory;  public  class  Telunsu implements   Milk {@Override  public   String GetName () { return " ; }}
 Package pattern.factory;  Public class Implements Milk {    @Override    public  String getName () {        return ' Erie ';    }} 
ImportPattern.factory.TeLunSu;ImportPattern.factory.YiLi; Public classSimplefactory { PublicMilk getmilk (String name) {Milk Milk=NULL; if("Clenbuterol". Equals (name)) {Milk=NewTelunsu (); }         if("Mengniu". Equals (name)) {Milk=NewMengNiu (); }         if("Erie". Equals (name)) {Milk=NewYiLi (); }         returnmilk; }}
 Package pattern.factory.simple;  Public class simplefactorytest {    publicstaticvoid  main (string[] args) {         // inform the factory        of the user's required products // mainly hides the product creation process, the user does not need to know the milk the creation process        Simplefactory simplefactory=New  simplefactory ();        System.out.println (Simplefactory.getmilk ("Clenbuterol"));}    }

Simple factory through code seems to actually want a small workshop model, what products of milk have, division of labor is not very clear, all products share a factory, and the method requires a parameter manual input, increase the probability of error.

Factory method
 public  class  mengniufactory implements   Factory {@Override  public   Milk Getmilk () { return new   MengNiu (); }}
 package   Pattern.factory.func;  import   Pattern.factory.Milk;  import   Pattern.factory.TeLunSu;  public  class  Telunsufactory implements   Factory {@Override public   Milk Getmilk () { return  new   Telunsu (); }}
 package   Pattern.factory.func;  import   Pattern.factory.Milk;  import   pattern.factory.YiLi;  public  class  Yilifactory implements   Factory {@Override  public   Milk Getmilk () { return  new   YiLi (); }}
 package   Pattern.factory.func;  import   Pattern.factory.Milk;  public  interface   Factory { public   Milk Getmilk ();}  
 Package Pattern.factory.func;  Public class factorytest {    publicstaticvoid  main (string[] args) {        Factory Factory=new  mengniufactory ();        System.out.println (Factory.getmilk ());}    }

Factory methods like factory-style production, a product corresponding to a factory and then assembled, this comparison specification, can be used for batch standardization of large-scale production, but also can be seen that users choose a product a bit inconvenient.

Abstract Factory
 Package Pattern.factory.abstr; Import Pattern.factory.Milk;  Public Abstract class abstractfactory {    publicabstract  Milk Gettelunsu ();      Public Abstract Milk Getyili ();      Public Abstract Milk Getmengniu ();}
 PackagePattern.factory.abstr;ImportPattern.factory.Milk;Importpattern.factory.func.MengNiuFactory;Importpattern.factory.func.TeLunSuFactory;Importpattern.factory.func.YiLiFactory; Public classMilkfactoryextendsabstractfactory {@Override PublicMilk Gettelunsu () {return Newtelunsufactory (). Getmilk (); } @Override PublicMilk Getyili () {return Newyilifactory (). Getmilk (); } @Override PublicMilk Getmengniu () {return Newmengniufactory (). Getmilk (); }}
 Package Pattern.factory.abstr;  Public class abstractfactorytest {    publicstaticvoid  main (string[] args) {        Abstractfactory abstractfactory=new  milkfactory ();        Abstractfactory.getmengniu ();    }}

Personal feeling abstract factory and factory method is very similar, but it in the factory method based on the re-encapsulation, flexibility is better, users can casually choose their own desired products, but also reflects the expansion of the code, abstract factory benefits are better than interfaces, because the abstract class can be used to deal with the logic of the Code , write some methods, look a bit like the assembly line mode, a line to produce a product.

Factory mode explaining

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.