Java设计模式-工厂方法模式

2020-03-16 23:25:18 浏览数 (2)

基本介绍

工厂方法模式:定义了一个创建对象的抽象方法,由子类决定要实例化的类。工厂方法模式将对象的实例化推迟到子类

需求说明

1)披萨的种类:如北京的奶酪pizza、北京的呼叫pizza或者是伦敦的奶酪pizza、伦敦的胡椒pizza

2)披萨的制作过程包括:prepare、bake、cut、box 3)完成披萨店订购功能

UML图

代码

代码语言:javascript复制
// 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;
    }
}
代码语言:javascript复制
// BJCheesePizza类继承Pizza
public class BJCheesePizza extends Pizza {

    @Override
    public void prepare() {
        setName("BJCheesePizza");
        System.out.println("BJCheesePizza 准备原材料");
    }
}
代码语言:javascript复制
// BJPepperPizza类继承Pizza类
public class BJPepperPizza extends Pizza {
    @Override
    public void prepare() {
        setName("BJPepperPizza");
        System.out.println("BJPepperPizza 准备原材料");
    }
}
代码语言:javascript复制
// LDCheesePizza 类继承Pizza
public class LDCheesePizza extends Pizza {
    @Override
    public void prepare() {
        setName("LDCheesePizza");
        System.out.println("LDCheesePizza 准备原材料");
    }
}
代码语言:javascript复制
// LDPepperPizza 类继承Pizza
public class LDPepperPizza extends Pizza {
    @Override
    public void prepare() {
        setName("LDPepperPizza");
        System.out.println("LDPepperPizza 准备原材料");
    }
}
代码语言:javascript复制
// OrderPizza抽象类
public abstract class OrderPizza {
    abstract Pizza createPizza(String orderType);
    public OrderPizza(){
        Pizza pizza = null;
        String orderType;
        do{
            orderType = getType();
            pizza = createPizza(orderType);
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
        }while (true);
    }
    private String getType() {
        try{
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza : ");
            String str = strin.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
代码语言:javascript复制
// BJOrderPizza 类继承OrderPizza并实现createPizza()方法
public class BJOrderPizza extends OrderPizza{
    Pizza createPizza(String orderType){
        Pizza pizza = null;
        if(orderType.equals("cheese")){
            pizza = new BJCheesePizza();
        }else if(orderType.equals("pepper")){
            pizza = new BJPepperPizza();
        }
        return pizza;
    }
}
代码语言:javascript复制
// LDOrderPizza类继承OrderPizza 并实现createPizza()方法
public class LDOrderPizza extends OrderPizza {
    Pizza createPizza(String orderType){
        Pizza pizza = null;
        if(orderType.equals("cheese")){
            pizza = new LDCheesePizza();
        }else if(orderType.equals("pepper")){
            pizza = new LDPepperPizza();
        }
        return pizza;
    }
}
代码语言:javascript复制
//PizzaStore类
public class PizzaStore {
    private static String getCountryName(){
        try{
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input CountryName : ");
            String str = strin.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
    public static void main(String[] args) {
        String loc = getCountryName();
        if(loc.equals("bj")){
            new BJOrderPizza();
        }else {
            new LDOrderPizza();
        }
    }
}

0 人点赞