简单工厂模式

2022-05-09 13:46:02 浏览数 (2)

需求

        看一个披萨项目:要便于披萨种类扩展,要便于维护

        1、披萨种类有很多(比如:GreekPizza、CheesePizza等)

        2、披萨的制作有prepare,bake,cut,box

        3、完成披萨店订购功能

基本介绍

       1)简单工厂模式是属于创建型模式,是工厂模式的一种。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式        2)简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为(代码)        

       3)在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会使用到工厂模式.

代码:

        pizza抽象类
代码语言:javascript复制
/**
 * @ProjectName: DesignPattern
 * @Package: simplefactory
 * @Author: huat
 * @Date: 2020/6/16 16:57
 * @Version: 1.0
 */
public abstract class Pizza {
    //披萨名称
    private String name;
    //因为每个披萨原材料不同,所以此处使用抽象方法
    //准备原材料
    public abstract void perpare();
    //烤披萨
    public void bake(){
        System.out.println("bake...." name);
    }
    //切披萨
    public void cut(){
        System.out.println("cut...." name);
    }
    //打包披萨
    public void box(){
        System.out.println("box...." name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
        pizza对象类
代码语言:javascript复制
/**
 * @ProjectName: DesignPattern
 * @Package: simplefactory
 * @Author: huat
 * @Date: 2020/6/16 17:45
 * @Version: 1.0
 */
public class GreekPizza extends Pizza {
    @Override
    public void perpare() {
        System.out.println("给希腊披萨准备原材料");
    }
}
代码语言:javascript复制
/**
 * @ProjectName: DesignPattern
 * @Package: simplefactory
 * @Author: huat
 * @Date: 2020/6/16 17:01
 * @Version: 1.0
 */
public class CheesePizza extends Pizza {
    @Override
    public void perpare() {
        System.out.println("给奶酪披萨准备原材料");
    }
}
        pizza工厂类
代码语言:javascript复制
import simplefactory.pizza.CheesePizza;
import simplefactory.pizza.GreekPizza;
import simplefactory.pizza.Pizza;

/**
 * @ProjectName: DesignPattern
 * @Package: simplefactory
 * @Author: huat
 * @Date: 2020/6/17 8:46
 * @Version: 1.0
 */
//简单工厂类
public class SimpleFactory {
    public Pizza createPizza(String type){
        System.out.println("使用简单工厂模式");
        Pizza pizza=null;
        if("cheesePizza".equalsIgnoreCase(type)){
            pizza=new CheesePizza();
            pizza.setName("奶酪披萨");
        }else if("greekPizza".equalsIgnoreCase(type)){
            pizza=new GreekPizza();
            pizza.setName("希腊披萨");
        }
        return pizza;
    }
}
        订购披萨
代码语言:javascript复制
import simplefactory.SimpleFactory;
import simplefactory.pizza.Pizza;

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

/**
 * @ProjectName: DesignPattern
 * @Package: simplefactory.order
 * @Author: huat
 * @Date: 2020/6/16 17:55
 * @Version: 1.0
 */
public class OrderPizza {
    //初始化工厂类
    SimpleFactory simpleFactory;
    //披萨
    Pizza pizza=null;
    public void setSimpleFactory(SimpleFactory simpleFactory) throws IOException {
        String pizzaType="";

        this.simpleFactory = simpleFactory;
        //循环订购披萨
        do {
            pizzaType=getType();
            pizza=this.simpleFactory.createPizza(pizzaType);
            if(null!=pizza){
                pizza.perpare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }else{
                System.out.println("暂无此pizza");
                break;
            }
        }while (true);
    }
    //选择订购的披萨类型
    private String getType() throws IOException {
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入你要订购的披萨");
        String str=bufferedReader.readLine();
        return str;
    }

}
        测试类
代码语言:javascript复制
import simplefactory.SimpleFactory;
import simplefactory.order.OrderPizza;

import java.io.IOException;

/**
 * @ProjectName: DesignPattern
 * @Package: simplefactory.test
 * @Author: huat
 * @Date: 2020/6/17 14:46
 * @Version: 1.0
 */
public class TestPizza {
    public static void main(String[] args) throws IOException {
        OrderPizza orderPizza=new OrderPizza();
        orderPizza.setSimpleFactory(new SimpleFactory());
    }
}

0 人点赞