Simple factory mode of design mode in 23

Simple factory mode

Look at a specific need

Look at a pizza project: it should be convenient for the expansion of pizza types and maintenance

  1. There are many kinds of pizza (such as greenpizza, cheese pizza, etc.)

  1. Pizza making includes prepare, bake, cut and box

  1. Complete the pizza shop ordering function.

Use the traditional way to complete

  1. Train of thought analysis (class diagram)

 

  1. Code demonstration

Abstract pizza class:

package com.atguigu.factory.simplefactory.pizzastore.pizza;

//take Pizza Make class abstract
public abstract class Pizza {
    protected String name; //name

    //Prepare raw materials, Different pizza is different, so we make it abstract
    public abstract void prepare();

    
    public void bake() {
        System.out.println(name + " baking;");
    }

    public void cut() {
        System.out.println(name + " cutting;");
    }

    //pack
    public void box() {
        System.out.println(name + " boxing;");
    }

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

Cheese pizza, inheriting Abstract pizza class:

package com.atguigu.factory.simplefactory.pizzastore.pizza;

public class CheesePizza extends Pizza {

    @Override
    public void prepare() {
        // TODO Auto-generated method stub
        System.out.println(" Prepare raw materials for making cheese pizza ");
    }

}

Greek pizza, inherited pizza class:

package com.atguigu.factory.simplefactory.pizzastore.pizza;

public class GreekPizza extends Pizza {

    @Override
    public void prepare() {
        // TODO Auto-generated method stub
        System.out.println(" Prepare raw materials for Greek pizza ");
    }

}

Order pizza:

package com.atguigu.factory.simplefactory.pizzastore.order;

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


import com.atguigu.factory.simplefactory.pizzastore.pizza.Pizza;

public class OrderPizza {

    // constructor 
    public OrderPizza() {
        Pizza pizza = null;
        String orderType; // Type of pizza ordered
        do {
            orderType = getType();
            if (orderType.equals("greek")) {
                pizza = new GreekPizza();
                pizza.setName(" Greek pizza ");
            } else if (orderType.equals("cheese")) {
                pizza = new CheesePizza();
                pizza.setName(" Cheese pizza ");
            }else {
                break;
            }
            //output pizza Production process
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
            
        } while (true);
    }
        // Write a method to get the type of pizza customers want to order
    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 "";
        }
    }

}
    

client:

package com.atguigu.factory.simplefactory.pizzastore.order;

//It is equivalent to a client and sends out an order
public class PizzaStore {

    public static void main(String[] args) {
        new OrderPizza();
    }

}

Advantages and disadvantages of traditional methods:

  1. The advantages are easy to understand, simple and easy to operate.

  1. The disadvantage is that it violates the ocp principle of design pattern, that is, it is open to extension and closed to modification. That is, when we add new functions to the class, we try not to modify the code, or modify the code as little as possible

  1. For example, if we want to add a new Pizza (Pepper Pizza), we need to make the following modifications

code:

Order pizza:

package com.atguigu.factory.simplefactory.pizzastore.order;

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


import com.atguigu.factory.simplefactory.pizzastore.pizza.Pizza;

public class OrderPizza {

    // constructor 
    public OrderPizza() {
        Pizza pizza = null;
        String orderType; // Type of pizza ordered
        do {
            orderType = getType();
            if (orderType.equals("greek")) {
                pizza = new GreekPizza();
                pizza.setName(" Greek pizza ");
            } else if (orderType.equals("cheese")) {
                pizza = new CheesePizza();
                pizza.setName(" Cheese pizza ");
            } else if (orderType.equals("pepper")) {
                pizza = new PepperPizza();
                pizza.setName("Pepper pizza");
            } else {
                break;
            }
            //output pizza Production process
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
            
        } while (true);
    }
        // Write a method to get the type of pizza customers want to order
    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 "";
        }
    }

}
    

Pepper pizza:

package com.atguigu.factory.simplefactory.pizzastore.pizza;

public class PepperPizza extends Pizza {

    @Override
    public void prepare() {
        // TODO Auto-generated method stub
        System.out.println(" Prepare raw materials for pepper pizza ");
    }

}

  4. Analysis of improvement ideas

Analysis: it is acceptable to modify the code, but if we also have the code to create Pizza in other places, it means that we also need to modify it, and the code to create Pizza often has many places.

Idea: encapsulate the created Pizza object into a class, so that when we have a new Pizza type, we only need to modify the class, and other codes that create Pizza objects do not need to be modified. - > Simple factory mode

Basic introduction to simple factory mode

  1. The simple factory mode belongs to the creation mode, which is a kind of factory mode. The simple factory pattern is a factory object that determines which product class instance to create. The simple factory pattern is the simplest and most practical pattern in the factory pattern family

  1. Simple factory pattern: defines a class for creating objects, which encapsulates the behavior (code) of instantiating objects

  1. In software development, when we will use a lot of to create a, a class or a batch of objects, we will use the factory pattern

Use of simple factory mode

  1. Design scheme of simple factory pattern: define a class that can instantiate Pizaa objects and encapsulate the code to create objects.

  2. Train of thought analysis

 

 

code:

Simple factory class:

package com.atguigu.factory.simplefactory.pizzastore.order;

import com.atguigu.factory.simplefactory.pizzastore.pizza.CheesePizza;
import com.atguigu.factory.simplefactory.pizzastore.pizza.GreekPizza;
import com.atguigu.factory.simplefactory.pizzastore.pizza.PepperPizza;
import com.atguigu.factory.simplefactory.pizzastore.pizza.Pizza;

//Simple factory class
public class SimpleFactory {

    //More orderType Return the corresponding Pizza object
    //Simple factory mode is also called static factory mode 
    public Pizza createPizza(String orderType) {

        Pizza pizza = null;

        System.out.println("Use simple factory mode");
        if (orderType.equals("greek")) {
            pizza = new GreekPizza();
            pizza.setName(" Greek pizza ");
        } else if (orderType.equals("cheese")) {
            pizza = new CheesePizza();
            pizza.setName(" Cheese pizza ");
        } else if (orderType.equals("pepper")) {
            pizza = new PepperPizza();
            pizza.setName("Pepper pizza");
        }
        
        return pizza;
    }
}

Order pizza:

package com.atguigu.factory.simplefactory.pizzastore.order;

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


import com.atguigu.factory.simplefactory.pizzastore.pizza.Pizza;

public class OrderPizza {

    //Define a simple factory object
    SimpleFactory simpleFactory;
    Pizza pizza = null;
    
    //constructor 
    public OrderPizza(SimpleFactory simpleFactory) {
        setFactory(simpleFactory);
    }
    
    public void setFactory(SimpleFactory simpleFactory) {
        String orderType = ""; //User entered
        
        this.simpleFactory = simpleFactory; //Set simple factory object
        
        do {
            orderType = getType(); 
            pizza = this.simpleFactory.createPizza(orderType);
            
            //output pizza
            if(pizza != null) { //Order succeeded
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println(" Failed to order pizza ");
                break;
            }
        }while(true);
    }
    
    // Write a method to get the type of pizza customers want to order
    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 "";
        }
    }

}

client:

package com.atguigu.factory.simplefactory.pizzastore.order;

//It is equivalent to a client and sends out a subscription
public class PizzaStore {

    public static void main(String[] args) {
        
        //Use simple factory mode
        new OrderPizza(new SimpleFactory());
        System.out.println("~~Exit program~~");
    }

}

 

Added by pelleas1022 on Sat, 22 Jan 2022 18:01:48 +0200