Design mode - factory mode

Posted by kuma on Sun, 20 Feb 2022 13:32:04 +0100

1, Pizza order

1. Create pizza

  1. Abstract class pizza
//abstract class
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;
    }
}
  1. Cheese pizza
public class CheesePizza extends Pizza{
    @Override
    public void prepare() {
        System.out.println("Prepare raw materials for making cheese pizza");
    }
}
  1. Greek pizza
public class GreekPizza extends Pizza{
    @Override
    public void prepare() {
        System.out.println("Prepare raw materials for Greek pizza");
    }
}

2. Order

  1. Order 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 making process
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
        }while (true);

    }


    // Get the type of pizza ordered by the customer
    public 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 "";
        }
    }
}
  1. Client, initiate pizza ordering task
// Client, initiate pizza ordering task
public class PizzaStore {
    public static void main(String[] args) {
        new OrderPizza();
    }
}

2, Simple factory mode

1. What is the simple factory model

  1. 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
  2. Simple factory pattern: defines a class for creating objects, which encapsulates the behavior of instantiating objects
  3. In software development, when we will use a lot of to create a certain, a certain class or a batch of objects, we will use the factory mode.

2. Use simple factory mode

  1. Create factory class
//Simple factory class
public class SimpleFactory {

    // Returns the corresponding Pizza object according to the orderType
    public Pizza createPizza(String orderType){
        System.out.println("Use simple factory mode");

        Pizza pizza = null;

        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 {
            return null;
        }
        return pizza;
    }
}

  1. Modification order
public class OrderPizza {
    //Define a simple factory object
    SimpleFactory simpleFactory;
    Pizza pizza = null;

    //constructor 
    public OrderPizza(SimpleFactory simpleFactory){
        setSimpleFactory(simpleFactory);
    }

    public void setSimpleFactory(SimpleFactory simpleFactory){
        String orderType = ""; //User input

        this.simpleFactory = simpleFactory; //Set simple factory object

        do {
            orderType = getType();
            pizza = this.simpleFactory.createPizza(orderType);

            //Enter pizza information
            if (pizza != null){
                //Output pizza making process
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }else {
                System.out.println("Failed to order pizza");
                break;
            }
        }while (true);
    }

    // Get the type of pizza ordered by the customer
    public 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 "";
        }
    }
}

  1. Client, initiate pizza ordering task
// Client, initiate pizza ordering task
public class PizzaStore {
    public static void main(String[] args) {
        //new OrderPizza();

        //Use simple factory mode
        new OrderPizza(new SimpleFactory());
        System.out.println("--Exit program--");
    }
}

3, Factory method model

1. Mode introduction

  1. The instantiation function of pizza project is abstracted into abstract methods, which are implemented in different taste ordering subclasses.
  2. Factory method pattern: defines an abstract method to create an object. The subclass determines the class to be instantiated. The factory method pattern delays the instantiation of the object to the subclass

2. Use factory method mode

  1. Creating pizza and implementing classes
//abstract class
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;
    }
}
// Cheese pizza in Beijing
public class BJCheesePizza extends Pizza{
    @Override
    public void prepare() {
        setName("Cheese pizza in Beijing");
        System.out.println("Beijing cheese pizza, prepare raw materials");
    }
}
// Pepper pizza in Beijing
public class BJPepperPizza extends Pizza{
    @Override
    public void prepare() {
        setName("Pepper pizza in Beijing");
        System.out.println("Pepper pizza in Beijing, preparing raw materials");
    }
}
// Cheese pizza in London
public class LDCheesePizza extends Pizza{
    @Override
    public void prepare() {
        setName("Cheese pizza in London");
        System.out.println("Cheese pizza in London, preparing raw materials");
    }
}
// Pepper pizza in London
public class LDPepperPizza extends Pizza{
    @Override
    public void prepare() {
        setName("Pepper pizza in London");
        System.out.println("Pepper pizza in London, preparing raw materials");
    }
}
  1. Create an abstract class for ordering pizza
public abstract class OrderPizza {

    //Define an abstract method, createPizza, and let each factory subclass implement it by itself
    abstract Pizza createPizza(String orderType);

    //constructor 
    public OrderPizza() {
        Pizza pizza = null;
        String orderType; //Type of pizza ordered
        do {
            orderType = getType();
            pizza = createPizza(orderType); // Abstract methods are completed by factory subclasses

            //Production process of pizza
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();

        } while (true);
    }

    // Get the type of pizza ordered by the customer
    public 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 "";
        }
    }
}

  1. Abstract class for ordering pizza
public class BJOrderPizza extends OrderPizza{

    @Override
    Pizza createPizza(String orderType) {
        Pizza pizza = null;
        if (orderType.equals("cheese")){
            pizza = new BJCheesePizza();
        } else if (orderType.equals("pepper")){
            pizza = new BJPepperPizza();
        }
        return pizza;
    }
}

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;
    }
}
  1. Test class
// Client, initiate pizza ordering task
public class PizzaStore {
    public static void main(String[] args) {
        //Create all kinds of pizza with Beijing flavor
        new BJOrderPizza();
    }
}

4, Abstract factory pattern

1. Basic introduction

  1. The abstract factory pattern defines an interface for creating clusters of related or dependent objects without specifying specific classes.
  2. Abstract factory pattern can integrate simple factory pattern and factory method pattern.
  3. From the design level, abstract factory mode is the improvement of simple factory mode
  4. The factory is abstracted into two layers, absfactory (Abstract Factory) and the factory subclass of concrete implementation.

2. Use abstract factory pattern

  1. Pizza
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;
    }
}
// Cheese pizza in Beijing
public class BJCheesePizza extends Pizza {
    @Override
    public void prepare() {
        setName("Cheese pizza in Beijing");
        System.out.println("Beijing cheese pizza, prepare raw materials");
    }
}
// Pepper pizza in Beijing
public class BJPepperPizza extends Pizza {
    @Override
    public void prepare() {
        setName("Pepper pizza in Beijing");
        System.out.println("Pepper pizza in Beijing, preparing raw materials");
    }
}
// Cheese pizza in London
public class LDCheesePizza extends Pizza {
    @Override
    public void prepare() {
        setName("Cheese pizza in London");
        System.out.println("Cheese pizza in London, preparing raw materials");
    }
}
// Pepper pizza in London
public class LDPepperPizza extends Pizza {
    @Override
    public void prepare() {
        setName("Pepper pizza in London");
        System.out.println("Pepper pizza in London, preparing raw materials");
    }
}
  1. Abstract factory
// Abstract layer of abstract factory pattern
public interface AbsFactory {

    // Let the factory subclass realize
    public Pizza createPizza(String orderType);
}
  1. Subclass of abstract factory
// Factory subclass
public class BJFactory implements AbsFactory{
    @Override
    public Pizza createPizza(String orderType) {
        System.out.println("Use abstract factory pattern");
        Pizza pizza = null;
        if (orderType.equals("cheese")){
            pizza = new BJCheesePizza();
        } else if (orderType.equals("pepper")){
            pizza = new BJPepperPizza();
        }
        return pizza;
    }
}
// Factory subclass
public class LDFactory implements AbsFactory{
    @Override
    public Pizza createPizza(String orderType) {
        System.out.println("Use abstract factory pattern");
        Pizza pizza = null;
        if (orderType.equals("cheese")){
            pizza = new LDCheesePizza();
        } else if (orderType.equals("pepper")){
            pizza = new LDPepperPizza();
        }
        return pizza;
    }
}

  1. Ordering
public class OrderPizza {

    AbsFactory factory;

    // constructor 
    public OrderPizza(AbsFactory factory){
        setFactory(factory);
    }

    private void setFactory(AbsFactory factory){
        Pizza pizza = null;
        String orderType = ""; //User input
        this.factory = factory;

        do {
            orderType = getType();
            pizza = factory.createPizza(orderType);

            if (pizza != null){
                // Output information
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }else {
                System.out.println("Order failed");
                break;
            }

        } while (true);
    }

    // Types of pizza ordered by customers
    public 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 "";
        }
    }
}

  1. test
// Client, initiate pizza ordering task
public class PizzaStore {
    public static void main(String[] args) {
        //Create all kinds of pizza with Beijing flavor
        new OrderPizza(new BJFactory());
    }
}

Topics: Java Design Pattern