[Design Pattern] Factory Design Pattern (Factory Method Pattern)

Factory method pattern

A new requirement
For complete requirements, please see: Simple Factory Pattern

New requirements for the pizza project: When ordering pizza, customers can order different flavors of pizza, such as: Beijing cheese pizza, Beijing pepper pizza or London cheese pizza, London pepper pizza.

Thought 1

Use the simple factory pattern to create different simple factory classes, such as: BJPizzaSimpleFactory, 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, it is not particularly good

Thought 2

Use the factory method pattern

Introduction to Factory Method Pattern

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

Factory Method Pattern: Defines an abstract method for creating objects, and the subclass determines the class to be instantiated. The Factory Method pattern defers object instantiation to subclasses.

Code example:

pizza:

package com.lango.factory.factorymethod.pizzastore.pizza;

/**
 * @author Lango
 * @version 1.0
 */
// Make the Pizza class an abstract class
public abstract class Pizza {<!-- -->
    protected String name; // name

    // Prepare raw materials, different pizzas are different, therefore, make it an abstract method
    public abstract void prepare();

    // make
    public void bake() {<!-- -->
        System.out.println(name + " baking;");
    }

    // cut
    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;
    }
}
package com.lango.factory.factorymethod.pizzastore.pizza;

/**
 * @author Lango
 * @version 1.0
 */
public class BJCheesePizza extends Pizza {<!-- -->

    @Override
    public void prepare() {<!-- -->
        setName("Beijing Cheese Pizza");
        System.out.println("Prepare raw materials for Beijing Cheese Pizza");
    }
}
package com.lango.factory.factorymethod.pizzastore.pizza;

/**
 * @author Lango
 * @version 1.0
 */
public class BJPepperPizza extends Pizza {<!-- -->
    @Override
    public void prepare() {<!-- -->
        setName("Pepper Pizza in Beijing");
        System.out.println("Prepare raw materials for Beijing Pepper Pizza");
    }
}
package com.lango.factory.factorymethod.pizzastore.pizza;

/**
 * @author Lango
 * @version 1.0
 */
public class LDCheesePizza extends Pizza {<!-- -->

    @Override
    public void prepare() {<!-- -->
        setName("London Cheese Pizza");
        System.out.println("Prepare ingredients for London Cheese Pizza");
    }
}
package com.lango.factory.factorymethod.pizzastore.pizza;

/**
 * @author Lango
 * @version 1.0
 */
public class LDPepperPizza extends Pizza {<!-- -->
    @Override
    public void prepare() {<!-- -->
        setName("Pepper Pizza in Beijing");
        System.out.println("Prepare raw materials for Beijing Pepper Pizza");
    }
}

order:

package com.lango.factory.factorymethod.pizzastore.order;


import com.lango.factory.factorymethod.pizzastore.pizza.Pizza;

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

/**
 * @author Lango
 * @version 1.0
 */
public abstract class OrderPizza {<!-- -->

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

    public OrderPizza() {<!-- -->
        Pizza pizza = null;
        String orderType; // The type of pizza ordered
        do {<!-- -->
            orderType = getType();
            pizza = createPizza(orderType); // abstract method, implemented by factory subclasses
            if (pizza != null) {<!-- -->
                // output the pizza making process
                pizza. prepare();
                pizza. bake();
                pizza. cut();
                pizza. box();
            } else {<!-- -->
                return;
            }
        } while (true);
    }

    // Write a method to get the type of pizza the customer wants to order
    private String getType() {<!-- -->
        try {<!-- -->
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza type: ");
            return bufferedReader. readLine();
        } catch (IOException e) {<!-- -->
            e.printStackTrace();
            return "";
        }
    }
}
package com.lango.factory.factorymethod.pizzastore.order;

import com.lango.factory.factorymethod.pizzastore.pizza.BJCheesePizza;
import com.lango.factory.factorymethod.pizzastore.pizza.BJPepperPizza;
import com.lango.factory.factorymethod.pizzastore.pizza.Pizza;


/**
 * @author Lango
 * @version 1.0
 */
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;
    }
}
package com.lango.factory.factorymethod.pizzastore.order;

import com.lango.factory.factorymethod.pizzastore.pizza.LDCheesePizza;
import com.lango.factory.factorymethod.pizzastore.pizza.LDPepperPizza;
import com.lango.factory.factorymethod.pizzastore.pizza.Pizza;


/**
 * @author Lango
 * @version 1.0
 */
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;
    }
}

test:

package com.lango.factory.factorymethod.pizzastore.order;

/**
 * @author Lango
 * @version 1.0
 */
public class PizzaStore {<!-- -->
    public static void main(String[] args) {<!-- -->
        // Create various pizzas with Beijing taste
// new BJOrderPizza();

        // Create a variety of pizzas in London flavors
        new LDOrderPizza();
    }
}