设计模式之:简单工厂

有一个案例来推导出简单工厂模式:

 Pizza 类

/**
 * @description: 披萨类(包含基础的 准备原料、制作、切割、打包等操作)
 * @author: abel.he
 * @date: 2023-08-24
 **/
public class Pizza {
    /**
     * pizza名称
     */
    private String pizzaName;

    /**
     *   准备原材料
     */
    public void prepare(){
        System.out.println(pizzaName + ": 准备原材料");
    }

    /**
     * 烘烤
     */
    void bake() {
        System.out.println(pizzaName + ": 烘烤");
    }

    /**
     * 剪切
     */
    void cut() {
        System.out.println(pizzaName + ": 剪切");
    }

    /**
     * 打包
     */
    void box() {
        System.out.println(pizzaName + ": 打包");
    }
    public void setPizzaName(String pizzaName){
        this.pizzaName = pizzaName;
    }

}
ChessPizza类:
/**
 * @description: 芝士披萨
 * @author: abel.he
 * @date: 2023-08-24
 **/
public class ChessPizza extends Pizza {



}
GreekPizza类
/**
 * @description: 希腊披萨
 * @author: abel.he
 * @date: 2023-08-24
 **/
public class GreekPizza extends Pizza{

    
}
OrderPizza
/**
 * @description: 蛋糕店
 * @author: abel.he
 * @date: 2023-08-24
 **/
public class OrderPizza {

    public static void main(String[] args) {
        Pizza pizza = null;
        do {
            String type = getType();
            if ("chess".equals(type)) {
                pizza = new ChessPizza();
                pizza.setPizzaName("芝士披萨");
            } else if ("greek".equals(type)) {
                pizza = new GreekPizza();
                pizza.setPizzaName("希腊披萨");
            } else {
                System.out.println("订单结束");
                break;
            }

            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
        } while (true);
    }

    private static String getType() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想制作的披萨:");
        return scanner.nextLine();
    }

}

 优缺点说明:

  • 优点:容易理解,简单易操作
  • 缺点:违反了opc原则,对扩展开放,对修改关闭。即当我们增加新的功能时,尽量不修改代码,或者尽量少修改代码
    • 比如我们增加一个新的Pizza的种类(Pepper披萨),我们需要增加Pepper类,订单Pizza也需要增加判断
  • 改进思路:把创建的对象封装到一个类中,这样我们有新的Pizza种类时,只需要修改该类就可以了,其他创建到Pizza对象的代码就不需要修改了-》简单工厂模式

改成简单工程模式实现

 

Pizza
package org.example.creation_type.simple_factory.optimize;

/**
 * 披萨类
 *
 * @author abel.he
 * @since 2023-09-02
 */
public class Pizza {

    /**
     * pizza名称
     */
    private String pizzaName;

    /**
     *   准备原材料
     */
    public void prepare(){
        System.out.println(pizzaName + ": 准备原材料");
    }

    /**
     * 烘烤
     */
    void bake() {
        System.out.println(pizzaName + ": 烘烤");
    }

    /**
     * 剪切
     */
    void cut() {
        System.out.println(pizzaName + ": 剪切");
    }

    /**
     * 打包
     */
    void box() {
        System.out.println(pizzaName + ": 打包");
    }
    public void setPizzaName(String pizzaName){
        this.pizzaName = pizzaName;
    }


}

CheesePizza

 

public class CheesePizza extends Pizza {
    public CheesePizza() {
        super.setPizzaName("奶酪披萨");
    }

}

 

GreekPizza
public class GreekPizza extends Pizza {

    public GreekPizza() {
        setPizzaName("希腊披萨");
    }

}

 SimpleFactory

public class SimpleFactory {

    /**
     * 返回pizza数据
     *
     * @param type 要创建的pizza种类
     * @return
     */
    public Pizza createPizza(String type) {
        System.out.println("使用简单工厂模式");
        Pizza pizza = null;
        if ("chess".equals(type)) {
            pizza = new CheesePizza();
            pizza.setPizzaName("芝士披萨");
        } else if ("greek".equals(type)) {
            pizza = new GreekPizza();
            pizza.setPizzaName("希腊披萨");
        }
        return pizza;
    }

}

 OrderPizza1

public class OrderPizza1 {

    // 定义一个简单工厂对象
    SimpleFactory simpleFactory;
    Pizza pizza = null;

    // 构造器传参
    public OrderPizza1(SimpleFactory simpleFactory) {
        setFactory(simpleFactory);
    }

    private void setFactory(SimpleFactory simpleFactory) {
        // 用户属于
        String orderType = "";

        do {
            orderType = getType();
            Pizza pizza = simpleFactory.createPizza(orderType);
            if (Objects.nonNull(pizza)) {
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("披萨订购失败");
                break;
            }
        } while (true);
    }

    /**
     * 获取要订阅的pizza
     *
     * @return pizza
     */
    private String getType() {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        try {
            System.out.print("请输入要订购的披萨:");
            String pizzaType = reader.readLine();
            System.out.println("要订阅的pizza种类:" + pizzaType);
            return pizzaType;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
PizzaStore
public class PizzaStore {

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

}

  

posted @ 2023-08-24 07:25  译林  阅读(7)  评论(0编辑  收藏  举报