Java design pattern------factory pattern-------factory method pattern

Factory method pattern

Let's first look at a requirement: when customers order pizza, they can order pizza with different flavors, such as cheese pizza in Henan, pepper pizza in Henan, or cheese pizza in London, or pepper pizza in London.

Ideas

Idea 1 : Use the simple factory pattern to create different simple factory classes , such as HNPizzaSimpleFactory,
LDPizzaSimpleFactory, etc. From the current case, it is also possible, but considering the scale of the project, as well as the maintainability and scalability of the software Not particularly good

Idea 2 : Use the factory method pattern

Introduction to Factory Method Pattern

Factory method pattern design scheme : Abstract the instantiation function of the pizza project into an abstract method, and implement it in different taste ordering subclasses.

Factory method pattern : defines an abstract method to create an object, and the subclass determines the class to be instantiated . The factory method pattern defers the instantiation of objects to subclasses.

Class Diagram

Insert picture description here

Our simple factory model is to create object instances directly in the factory , and the factory method model is to give the authority to create objects to his subclasses , HNOrderPizz (Henan Pizza) and LNOrderPizza (London Pizza)

Code

Pizza entity

/**
 * @author 王庆华
 * @version 1.0
 * @date 2020/12/21 20:06
 * @Description TODO
 * @pojectname 披萨抽象类    设置为抽象类
 */
public abstract class Pizza {
    
    
    //披萨名字
    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");
    }

    public void setName(String name) {
    
    
        this.name = name;
    }
}

4 sub-categories of Henan pizza and London pizza

/**
 * @author 王庆华
 * @version 1.0
 * @date 2020/12/25 20:47
 * @Description TODO
 * @pojectname 河南奶酪代码
 */
public class HNCheesePizza extends Pizza{
    
    
    @Override
    public void prepare() {
    
    
        setName("河南的奶酪披萨");
        System.out.println("给河南奶酪披萨准备原材料");
    }
}
/**
 * @author 王庆华
 * @version 1.0
 * @date 2020/12/25 20:49
 * @Description TODO
 * @pojectname 河南胡椒代码
 */
public class HNPepperPizza extends  Pizza {
    
    
    @Override
    public void prepare() {
    
    
        setName("河南的胡椒披萨");
        System.out.println("给河南胡椒披萨准备原材料");
    }
}
/**
 * @author 王庆华
 * @version 1.0
 * @date 2020/12/25 20:47
 * @Description TODO
 * @pojectname 伦敦奶酪代码
 */
public class LDCheesePizza extends Pizza{
    
    
    @Override
    public void prepare() {
    
    
        setName("伦敦的奶酪披萨");
        System.out.println("给伦敦奶酪披萨准备原材料");
    }
}
伦敦胡椒披萨也如此

OrderPizza order code

/**
 * @author 王庆华
 * @version 1.0
 * @date 2020/12/21 20:14
 * @Description TODO
 * @pojectname 订购披萨代码
 */
public abstract class OrderPizza {
    
    
    //定义一个抽象方法createPizza,让各个工厂子类自己实现
    abstract Pizza createPizza(String orderType);
    //构造器
    public OrderPizza(){
    
    
        Pizza pizza = null;
        String orderType ;//orderType用户输入pizza类型
        do {
    
    
            orderType = getType();//获取用户订购的pizza类型
            System.out.println(orderType);
            pizza = createPizza(orderType);//抽象方法,实际上又工厂子类完成
            //输出制作pizza信息
            //订购成功
            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 种类:");
            String str = strin.readLine();
            return str;
        } catch (IOException e) {
    
    
            e.printStackTrace();
            return "";
        }
    }
}

Two subclasses of OrderPizza

/**
 * @author 王庆华
 * @version 1.0
 * @date 2020/12/25 20:56
 * @Description TODO
 * @pojectname 河南披萨订单代码
 */
public class HNOrderPizza extends OrderPizza {
    
    
    @Override
    Pizza createPizza(String orderType) {
    
    
        Pizza pizza = null;
        if (orderType.equals("cheese")) {
    
    
            pizza = new HNCheesePizza();
        }else if (orderType.equals("pepper")){
    
    
            pizza = new HNPepperPizza();
        }
        return pizza;
    }
}
/**
 * @author 王庆华
 * @version 1.0
 * @date 2020/12/25 20:56
 * @Description TODO
 * @pojectname 伦敦披萨代码
 */
public class LDOrderPizza extends OrderPizza {
    
    
    @Override
    Pizza createPizza(String orderType) {
    
    
        Pizza pizza = null;
        if (orderType.equals("cheese")) {
    
    
            pizza = new LDCheesePizza();
        }else if (orderType.equals("pepper")){
    
    
            pizza = new LDPepperPizza();
        }
        return pizza;
    }
}
public class PizzaStore {
    
    
    public static void main(String[] args) {
    
    
        //创建河南口味的各种方法
        new HNOrderPizza();
    }
}
根据我们new的种类,我们就能得到不同的结果
input pizza 种类:
cheese
cheese
给河南奶酪披萨准备原材料
河南的奶酪披萨baking;
河南的奶酪披萨cutting;
河南的奶酪披萨boxing
input pizza 种类:
pepper
那我们要是 new LDOrderPizza();
那么输出的都是有关伦敦披萨的口味

to sum up

Let's summarize again. In our simple factory model, our idea is to directly let our OrderPizza order class , according to different orderType tastes, to new a different taste of pizza , and our method factory model is like this : In our method factory model, our OrderPizza order class is written as an abstract class with the createPiiza() abstract method . This abstract method is used to create pizza instance objects, but it is an abstract method , so our Henan pizza order And London pizza orders must inherit the main class OrderPizza . That is to say , we distribute the task of creating instance objects to different subclasses . The subclasses are originally different, and they can naturally make pizzas of different places, and then After the subclasses create different flavors of pizza in different places, they return to our OrderPizza main class , and then our main class outputs the corresponding information

Guess you like

Origin blog.csdn.net/qq_22155255/article/details/111708904