面向对象设计之建造者模式


建造者模式使用多个简单的对象一步一步构建成一个复杂的对象。可以将复杂对象的创建过程独立出来,简化逻辑。

直接从代码感受吧

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//考虑肯德基点餐,一个典型的套餐可以是一个汉堡和一杯冷饮。汉堡可以是素食汉堡或鸡肉汉堡,它们是包在纸盒中。冷饮可以是可口可乐或百事可乐,它们是装在瓶子中。

//先创建一个表示食物条目和食物包装的接口。
public interface Item {
public String name();
public Packing packing();
public float price();
}
public interface Packing {
public String pack();
}

//实现包装的实体类。
public class Wrapper implements Packing {
@Override
public String pack() {
return "Wrapper";
}
}
public class Bottle implements Packing {
@Override
public String pack() {
return "Bottle";
}
}

//抽象出汉堡类和冷饮类
public abstract class Burger implements Item {
@Override
public Packing packing() {
return new Wrapper();
}
}
public abstract class ColdDrink implements Item {
@Override
public Packing packing() {
return new Bottle();
}
}

//实现出汉堡和冷饮的具体类
public class VegBurger extends Burger {
@Override
public float price() {
return 25.0f;
}
@Override
public String name() {
return "Veg Burger";
}
}
public class ChickenBurger extends Burger {
@Override
public float price() {
return 50.5f;
}
@Override
public String name() {
return "Chicken Burger";
}
}
public class Coke extends ColdDrink {
@Override
public float price() {
return 30.0f;
}
@Override
public String name() {
return "Coke";
}
}
public class Pepsi extends ColdDrink {
@Override
public float price() {
return 35.0f;
}
@Override
public String name() {
return "Pepsi";
}
}

至此,组成几分套餐的基本成分就齐了,接下来我们再定义出套餐类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Meal {
private List<Item> items = new ArrayList<Item>();

public void addItem(Item item){
items.add(item);
}

public float getCost(){
float cost = 0.0f;
for (Item item : items) {
cost += item.price();
}
return cost;
}

public void showItems(){
for (Item item : items) {
System.out.print("Item : "+item.name());
System.out.print(", Packing : "+item.packing().pack());
System.out.println(", Price : "+item.price());
}
}
}

有了套餐元素和套餐类,我们就可以创建一个Builder对象,用于创建各种套餐。

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
public class MealBuilder {
public Meal prepareVegMeal (){
Meal meal = new Meal();
meal.addItem(new VegBurger());
meal.addItem(new Coke());
return meal;
}

public Meal prepareNonVegMeal (){
Meal meal = new Meal();
meal.addItem(new ChickenBurger());
meal.addItem(new Pepsi());
return meal;
}
}

//测试刚刚完成的MealBuilder
public class BuilderPatternDemo {
public static void main(String[] args) {
MealBuilder mealBuilder = new MealBuilder();

Meal vegMeal = mealBuilder.prepareVegMeal();
System.out.println("Veg Meal");
vegMeal.showItems();
System.out.println("Total Cost: " +vegMeal.getCost());

Meal nonVegMeal = mealBuilder.prepareNonVegMeal();
System.out.println("\n\nNon-Veg Meal");
nonVegMeal.showItems();
System.out.println("Total Cost: " +nonVegMeal.getCost());
}
}

有时候可能情况会更复杂一些,我们可能会这样定义MealBuilder:

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
public abstract class MealBuilder {
Meal m;
public void buildFood();
public void buildDrink();
public Meal getMeal() {
return m;
}
}
public class VegMealBuilder implements MealBuilder {
@Override
public void buildFood() {
m..addItem(new VegBurger());
}
@Override
public void buildDrink() {
m..addItem(new Coke());
}
}
public class NonVegMealBuilder implements MealBuilder {
@Override
public void buildFood() {
m..addItem(new ChickenBurger());
}
@Override
public void buildDrink() {
m..addItem(new Pepsi());
}
}

为了让各种不同的建造者工作起来,就需要引入监工(Director)角色,通过监工来实现使不同的建造者通过统一的流程创建出不同的产品。

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 class MealDirector {
MealBuilder builder;

MealDirector(MealBuilder b) {
this.builder = b;
}
public void setBuilder(MealBuilder b) {
this.builder = b;
}

public Meal buildMeal() {
this.builder.buildFood();
this.builder.buildDrink();
return this.builder.getMeal();
}
}

public class BuilderPatternDemo {
public static void main(String[] args) {
VegMealBuilder b1 = new VegMealBuilder();
NonVegMealBuilder b2 = new NonVegMealBuilder();

MealDirector director = new MealDirector(b1);
Meal vegMeal = director.buildMeal();
System.out.println("Veg Meal");
vegMeal.showItems();
System.out.println("Total Cost: " +vegMeal.getCost());

director.setBuilder(b2);
Meal nonVegMeal = director.buildMeal();
System.out.println("\n\nNon-Veg Meal");
nonVegMeal.showItems();
System.out.println("Total Cost: " +nonVegMeal.getCost());
}
}