[Design Mode] factory pattern - Abstract Factory

Brief introduction

According to the "head first design pattern" factory mode there are three: a static factory method , factory method , abstract factory. This article describes the abstract factory.

1 , the definition of: providing an interface for creating families of related or dependent objects, without the need to specify particular class.

2 , to meet the OO principles - Dependency Inversion principle: to rely on abstract and do not rely on specific classes.

3 , the structure pattern: factory class (abstract and concrete factories factory) and product classes (abstract and concrete products Products)

4 , the specific implementation ( complete code ):

The same is done pizza systems, factory method is based pizzeria abstract design, so that a pizza flavor of the region as a concrete creator classes to implement, we can be increased at a pizza flavor, but in this case, scalability is not enough because if a new pizza flavors appear with different materials, we still need to change the abstract product class (Pizza class ) and the specific product category be expanded. The abstract factory is based on raw materials abstracted as a factory design, so we need to create raw materials (abstract factory class), and then choose different raw materials for the production of pizza can be, if raw materials have increased or decreased, we just change the raw material class can, at the same time such a change does not affect the other classes. (The factory method, if the reduction in raw material, it may want to change a number of specific product categories involving this material, which is a relatively large changes)

  • Abstract factory class: defines an interface, all the concrete factory must implement this interface, all of the specific facility must implement this interface, which contains a set of methods used to produce the product.
public interface PizzaIngredientFactory{
    public Dough createDough();
    public Sauce createSauce();
    public Cheese createCheese();
    public Veggies[] createVeggies();
    public Pepperoni createPepperoni();
    public Clams createClam();
}
  • Concrete factory class: achieve different product families, to create a product, customers simply use one of the factory and completely without any product object is instantiated.
// create Chicago pizza plant material 
public  class ChicagoPizzaIngredientFactory the implements PizzaIngredientFactory {
     // dough 
    public Dough createDough () {
         return  new new ThickCrustDough (); 
    } 
    // sauces 
    public Sauce createSauce () {
         return  new new PlumTomatoSauce (); 
    } 
    // Cheese 
    public Cheese createCheese () {
         return  new new MozzarellaCheese (); 
    } 
    // vegetable 
    public Veggies [] createVeggies () { 
        Veggies Veggies []{= New new Spinach (), new new BlackOlives (), new new Eggplant ()};
         return Veggies; 
    } 
    // salami 
    public Pepperoni createPepperoni () {
         return  new new SlicedPepperoni (); 
    } 
    // clams 
    public Clams createClam () {
         return  new new FrozenClams (); 
    } 
} 

// making pizza in New York plant material 
public  class NYPizzaIngredientFactory the implements PizzaIngredientFactory {
     // dough 
    public dough createDough () {
        return new ThinCrustDough();
    }
    //酱料
    public Sauce createSauce(){
        return new MarinaraSauce();
    }
    //芝士
    public Cheese createCheese(){
        return new ReggianoCheese();
    }
    //蔬菜
    public Veggies[] createVeggies(){
        Veggies veggies[] = {new Garlic(),new Onion(),new Mushroom(),new RedPepper()};
        return veggies;
    }
    //意式腊肠
    public Pepperoni createPepperoni(){
        return new SlicedPepperoni();
    }
    //蛤蜊
    public Clams createClam(){
        return new FreshClams();
    }
}
  • Abstract Product categories:
// Starting material: Dough 
public  interface Dough {
     public String toString (); 
} 

// material: salami 
public  interface Pepperoni {
     public String toString (); 
}
  • Specific product categories:
@ Dough: Dough thickness 
public  class ThickCrustDough the implements Dough {
     public String toString () {
         return "Thick Crust Dough" ; 
    } 
} 

// Dough: Thin dough 
public  class ThinCrustDough the implements Dough {
     public String toString () {
         return "Thin Crust Dough " ; 
    } 
} 

// salami: salami slice 
public  class SlicedPepperoni the implements Pepperoni {
     public String toString () {
         return "Sliced Pepperoni";
    }
}
  • Client: Client code just abstract factory design, will automatically use the actual plant operation.
//纽约风味的披萨饼
public class NYPizzaStore extends PizzaStore{
    protected Pizza createPizza(String item){
        Pizza pizza = null;
        PizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory();

        if(item.equals("cheese")){
            pizza = new CheesePizza(ingredientFactory);
            pizza.setName("New York Style Cheese Pizza");
        }else if(item.equals("veggie")){
            pizza = new VeggiePizza(ingredientFactory);
            pizza.setName("New York Style Veggie Pizza");
        }else if(item.equals("clam")){
            pizza = new ClamPizza(ingredientFactory);
            pizza.setName("New York Style Clam Pizza");
        }else if(item.equals("pepperoni")){
            pizza = new PepperoniPizza(ingredientFactory);
            pizza.setName("New York Style Pepperoni Pizza");
        }
        return pizza;
    }
}

//芝加哥风味的披萨饼
public class ChicagoPizzaStore extends PizzaStore{
    protected Pizza createPizza(String item){
        Pizza pizza = null;
        PizzaIngredientFactory ingredientFactory = new ChicagoPizzaIngredientFactory();

        if(item.equals("cheese")){
            pizza = new CheesePizza(ingredientFactory);
            pizza.setName("Chicago Style Cheese Pizza");
        }else if(item.equals("veggie")){
            pizza = new VeggiePizza(ingredientFactory);
            pizza.setName("Chicago Style Veggie Pizza");
        }else if(item.equals("clam")){
            pizza = new ClamPizza(ingredientFactory);
            pizza.setName("Chicago Style Clam Pizza");
        }else if(item.equals("pepperoni")){
            pizza = new PepperoniPizza(ingredientFactory);
            pizza.setName("Chicago Style Pepperoni Pizza");
        }
        return pizza;
    }
}

5 , Abstract Factory advantages: You can put together a collection of a group of related products

6 , abstract factory Cons: adding new types of products more difficult.

7 , factory method and abstract factory difference:

  • The method of using the class factory, using the abstract factory objects
  • Factory methods to create objects using inheritance, and abstract factory to create objects by combining objects.

    Note: The customer is responsible for both decoupled from specific types, but different methods: factory method to create an object, a need to extend the class and override its factory methods; and the abstract factory provides a family of products used to create a type of abstract this type of sub-class defines a method products are produced, in order to use this facility, you must first instantiate it, and then pass some of it written for the abstract type code.

 

Reference material

[1] head first design pattern

Guess you like

Origin www.cnblogs.com/mj-selina/p/12488855.html
Recommended