设计模式之工厂模式

工厂模式分为两种:工厂方法模式和抽象工厂模式
一个Pizaa店的订单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class PizzaStore{
public Pizza orderPizza(String type){
Pizza p;
if("aa".equals(type))
p = new AAPizaa(); //AAPizza为Pizaa的子类
if("bb".equals(type))
p = BBPizaa(); //BBPizza为Pizaa的子类
if ...
...
...
return p;

}
}

简单工厂

简单工厂只是一种编程习惯,并不是真正的设计模式。他只是把类中实例化一个对象的工作给抽出来,用一个工厂类来负责对象的实例化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//实例化的工作被抽离出来
public class SimplePizzaFactory{
public SimplePizzaFactory(){};
public Pizza createPizza(String type){
if("aa".equals(type))
reutrn new AAPizza(); //AAPizza为Pizza的子类
if("bb".equals(type))
return BBPizza(); //BBPizza为Pizza的子类
if ...
...
}
}
public class PizzaStore{
SimplePizzaFactory spf;
public PizzaStore(SimplePizzaFactory spf){
this.spf = spf;
}
public Pizza orderPizza(String type){
Pizza p;
p = spf.createPizza(type);
...
...
return p;

}
}

工厂方法模式

工厂方法模式只是定义一个实例化子类的接口,具体子类的实例化由其派生类来决定实现
工厂方法模式定义:定义一个创建对象的接口,但由子类决定要实例化的类是哪一个类,工厂方法让类把实例化推迟到子类里。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public abstract class PizzaStore(){
abstract Pizza createPizza(String type); //工厂方法,具体pizza类的实例化由派生类来实现
public Pizza orderPizza(String type){
Pizza p;
p = createPizza(type);

}
}

//具体的对象实例化由派生类决定
public class AAPizzaStore() extends PizzaStore{
Pizza createPizza(String type){
System.out.println("I'm AAPizzaStore");
if("aa".equals(type))
reutrn new AA1Pizza(); //AA1Pizza为Pizza的子类

if("bb".equals(type))
return AA2Pizza(); //AA2Pizza为Pizza的子类

if ...
...
}

}

public class BBPizzaStore() extends PizzaStore{
Pizza createPizza(String type){
System.out.println("I'm BBPizzaStore");
if("aa".equals(type))
reutrn new BB1Pizza(); //BB1Pizza为Pizza的子类

if("bb".equals(type))
return BB2Pizza(); //BB2Pizza为Pizza的子类

if ...
...
}

}

抽象工厂模式

抽象工厂模式里面有很多个工厂方法,每一个工厂方法就是一个用来实例化类的接口
抽象工厂模式定义:提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类
抽象工厂允许客户使用抽象的接口来创建一组相关的产品,而不需要知道实际产出来的具体产品是什么。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//抽象工厂接口
public Interface PizzaIngredientFactory{
Dough createDough();
Sauce createSauce();
Cheese crateCheese();
Clam CreateClam();
...
}

//派生类
public class NYPizzaIngredientFactory implements PizzaIngredientFactory{
public Dough createDough(){
return NYDough(); //NYDough 为 Dough的子类
}

public Sauce createSauce(){
return NYSauce(); //NYSauce 为 Sauce的子类
}

public Cheese createCheese(){
return NYCheese(); //NYCheese 为 Cheese的子类
}

public Clam createClam(){
return NYClam(); //NYClam 为 Clam的子类
}
}


//派生类
public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory{
public Dough createDough(){
return ChicagoDough(); //ChicagoDough 为 Dough的子类
}

public Sauce createSauce(){
return ChicagoSauce(); //ChicagoSauce 为 Sauce的子类
}

public Cheese createCheese(){
return ChicagoCheese(); //ChicagoCheese 为 Cheese的子类
}

public Clam createClam(){
return ChicagoClam(); //ChicagoClam 为 Clam的子类
}
}