Design Patterns--Factory Patterns

Posted by dannyz on Sun, 12 May 2019 00:22:47 +0200

  • Derive Simple Factory Mode from Pizza Project

  • Factory Method Model

  • Abstract factory pattern

  • key point

  • Pizza project: to facilitate the expansion of pizza varieties, to facilitate maintenance, to be able to run the expansion
    • Pizza design:

    • Define an abstract class of Pizza, define four methods prepare() abstract method because each is different, but bake(), cut(), box() will be the same.

    • In this way, the content of the class will be changed, so the problem will be divided into the changing part and the unchanged part for the development of the extension.

  • Pizza factory design: if.. else if... else factory pattern resolves the problem of instantiation of objects, and places instantiation of classes somewhere for management

Simple factory model:

  • Define a class that creates an object and encapsulates the behavior of the instantiated object. To solve a large number of instances of certain classes, it is necessary to put a large number of instances of classes in a suitable place. Simple factory is a class that implements a large number of classes. The change part is extracted and the maintenance and extension are separated to reduce the coupling degree.
package gongchangmoshi;
/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-16
* Time: 12:19
*/
public abstract class Pizza {
    public String getName() {
        return name;
    }


    protected String name;
    public abstract void prepare();
    public void bake(){
        System.out.println(name + "baking");
    }
    public void cut(){
        System.out.println(name + "cutting");
    }
    public void box(){
        System.out.println(name + "boxing");
    }
}
package gongchangmoshi;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-16
* Time: 12:28
*/
public class SimplePizzaFactory {
    public Pizza createPizza(String orderType) {
        //Put the changes here.
        Pizza pizza = null;
        if (orderType.equals("greek")) {
            pizza = new GreekPizza();
        }
        return pizza;
    }
}

package gongchangmoshi;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-16
* Time: 12:31
*/
public class OrderPizza {
    SimplePizzaFactory simplePizzaFactory;
    public OrderPizza(SimplePizzaFactory  simplePizzaFactory){
        setSimplePizzaFactory(simplePizzaFactory)
    }
    public void setSimplePizzaFactory(SimplePizzaFactory simplePizzaFactory){
        Pizza pizza = null;
        String orderType;
        this.simplePizzaFactory =  simplePizzaFactory;
        do{
            orderType = gettype();
            pizza = simplePizzaFactory.createPizza(orderType);
            if(pizza!=null){
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }
        }while(true);
    }
    private String gettype() {
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(
                    System.in));
            System.out.println("input pizza type:");
            String str = strin.readLine();


            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}

Factory model:

  • The instantiation functions of pizza objects in pizza projects are abstracted into abstract methods, and the functions are implemented in different joining points.

  • The confusion of pizza franchises:

    • If you use a simple factory to do it, you will find that using a simple factory will create a simple factory everywhere, which is not conducive to code maintenance.
package gongchangmoshi.gongchangmoshi;


import gongchangmoshi.Pizza;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-17
* Time: 19:26
*/
public abstract class OrderPizza {
    public OrderPizza(){
        Pizza pizza = null;
        String ordertype;
        do{
            ordertype = getType();
            pizza = createPizza(ordertype);
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
        }while (true);


    }
    abstract Pizza createPizza(String orderType);
    private String getType() {
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(
                    System.in));
            System.out.println("input pizza type:");
            String str = strin.readLine();


            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
package gongchangmoshi.gongchangmoshi;

import gongchangmoshi.GreekPizza;
import gongchangmoshi.Pizza;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-17
* Time: 19:32
*/
public class LDOrderPizza extends OrderPizza {


    @Override
    Pizza createPizza(String orderType) {
        Pizza pizza = null;
        if(orderType.equals("gw")){
            pizza = new GreekPizza();
        }
        return pizza;
    }
}

The abstract factory pattern:

  • An interface is defined to create a family of related or dependent objects without specifying specific classes explicitly.

  • The common part of the factory is abstracted into interfaces, encapsulating different methods to realize the invariable function of the pizza family. The abstract factory is introduced into the order. When it is realized, it can be changed concretely, and then realized.

package gongchangmoshi.chouxianggongchang;


import gongchangmoshi.Pizza;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-17
* Time: 19:43
*/
public interface AbsFactory {
    public Pizza createPizza(String orderType);
}


package gongchangmoshi.chouxianggongchang;


import gongchangmoshi.GreekPizza;
import gongchangmoshi.Pizza;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-17
* Time: 19:46
*/
public class LDOrderPizza implements AbsFactory {
    @Override
    public Pizza createPizza(String orderType) {
        Pizza pizza = new GreekPizza();
        System.out.println("Making New York Style pizza");
        return pizza;
    }
}

package gongchangmoshi.chouxianggongchang;


import com.sun.org.apache.xpath.internal.operations.Or;
import gongchangmoshi.Pizza;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-17
* Time: 19:44
*/
public class OrderPizza {
    AbsFactory absFactory;
    public OrderPizza(AbsFactory absFactory){


    }
    public void setFactory(AbsFactory absFactory){
        Pizza pizza = null;
        String orderType;
        this.absFactory = absFactory;
        do{
            orderType = getType();
            pizza = absFactory.createPizza(orderType);
            if(pizza!=null){
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }
        }while (true);
    }
    private String getType() {
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(
                    System.in));
            System.out.println("input pizza type:");
            String str = strin.readLine();


            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
package gongchangmoshi.chouxianggongchang;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-17
* Time: 19:49
*/
public class PizzaStore {
    public static void main(String[] args) {
        OrderPizza orderPizza;
        orderPizza = new OrderPizza(new LDOrderPizza());
    }
}

Key points:

  • Dependence on abstract principles:
    • Objects do not hold references to specific classes, they are highly dependent, and variables = the return value of a factory

    • Don't let class processes come from concrete classes. Instead, inherit abstract classes and interfaces.

    • Instead of overriding the methods implemented in base classes, design them as interfaces or abstract classes

Topics: Mobile IntelliJ IDEA Java Apache