Creational design patterns factory pattern

Creating factories mode


Interface definition for creating an object, but let subclasses decide which class to instantiate
role  

  • Product definitions factory methods to create objects
  • ConcreteProduct achieve Product Interface, the definition of concrete products.
  • Factory methods to create a Product Creator is used to declare the type of object.
  • ConcreteCreator Creator factory method is implemented specifically for producing specific products ConcreteProduct.


Collaboration 
Creator relies on its subclasses define factory methods.
Feature

  • Abstract interfaces oriented product (Product); when adding a new type of product factory class for the abstract factory interface (Creator)
  • When the user needs to add a new class of product type (NewProduct), corresponding to only need to add a new factory class (NewProductFactory)
  • When users need to use the new product class, simply call the new factory class (NewProductFactory) to get new products to


Examples

// 产品 例如 果汁
public class Juicer{
    public Juicer MakeJuice()
}

 

//  苹果汁
public class appleJuicer extends  Juicer {}



 

// 销售员
public class Waiter{
      public void sell(Juicer juicer){}
}

 

Published 231 original articles · won praise 3 · Views 7990

Guess you like

Origin blog.csdn.net/qq_32265719/article/details/103902792