Posts 工厂模式
Post
Cancel

工厂模式

以制作披萨为例

1
2
3
4
5
6
7
8
public static Pizza orderPizza(String type){
    Pizza pizza = new Pizza();
    pizza.prepare();
    pizza.bake();
    pizza.cut();
    pizza.box();
    return pizza;
}

添加更多的Pizza,需要把Pizza定义为抽象类或接口,然后创建Pizza子类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static Pizza orderPizza(String type){
    Pizza pizza = null;
    if(type.equals("cheese")){
        pizza = new CheesePizza();
    }else if(type.equals("greek")){
        pizza = new GreekPizza();
    }else if(type.equals("pepperoni")){
        pizza = new PepperoniPizza();
    }
    pizza.prepare();
    pizza.bake();
    pizza.cut();
    pizza.box();
    return pizza;
}

如果需要添加新的Pizza或者去掉某些不畅销的Pizza,上面的代码需要做以下修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static Pizza orderPizza(String type){
    Pizza pizza = null;
    if(type.equals("cheese")){
        pizza = new CheesePizza();
    //去掉不畅销的Pizza
    // }else if(type.equals("greek")){
    //     pizza = new GreekPizza();
    }else if(type.equals("pepperoni")){
        pizza = new PepperoniPizza();
    //新添加两种Pizza
    }else if(type.equals("clam")){
        pizza = new ClamPizza();
    }else if(type.equals("veggie")){
        pizza = new VeggiePizza();
    }
    pizza.prepare();
    pizza.bake();
    pizza.cut();
    pizza.box();
    return pizza;
}

上面的代码无法让orderPizza()对修改关闭。

创建对象的代码是可变的,对象的方法调用是不可变的。这样我们把变化的抽取出来,我们定义一个简单工厂类来负责Pizza对象的创建。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class SimplePizzaFactory{
    public Pizza createPizza(String type){
        Pizza pizza = null;
        if(type.equals("cheese")){
            pizza = new CheesePizza();
        }else if(type.equals("pepperoni")){
            pizza = new PepperoniPizza();
        }else if(type.equals("clam")){
            pizza = new ClamPizza();
        }else if(type.equals("veggie")){
            pizza = new VeggiePizza();
        }
        return pizza;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class PizzaStore {
    SimplePizzaFactory factory;
    public PizzaStore(SimplePizzaFactory factory){
        this.factory = factory;
    }

    public Pizza orderPizza(String type){
        Pizza pizza = null;
        pizza = factory.createPizza(type);
        pizza.prepare();
        pizza.bake();
        pizza.cut();
        pizza.box();
        return pizza;
    }
}

简单工厂并不是真正的工厂模式。

随着业务的发展,可以选择在不同的地方开分店,每一个店都是一个创建Pizza的工厂,可以通过继承SimplePizzaFactory工厂创建新的工厂。

1
2
3
4
5
6
class NYPizzaFactory extends SimplePizzaFactory{
}
class ChicagoPizzaFactory extends SimplePizzaFactory{
}
class CaliforniaPizzaFactory extends SimplePizzaFactory{
}
1
2
3
4
5
6
NYPizzaFactory nyFactory = new NYPizzaFactory();
PizzaStore nyStore = new PizzaStore(nyFactory);
nyStore.orderPizza("veggie");
ChicagoPizzaFactory chicagoFactory = new ChicagoPizzaFactory();
PizzaStore chicagoStore = new PizzaStore(chicagoFactory);
chicagoStore.orderPizza("veggie");

定义工厂方法模式

工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。

依赖倒置原则

设计原则:要依赖抽象,不要依赖具体类

This post is licensed under CC BY 4.0 by the author.