创建型模式- 建造者模式
介绍
讲一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
使用多个简单的对象一步一步构建成一个复杂的对象
实例
- 创建一个表示食物条目和食物包装的接口
1
2
3
4
5
6
7
8
9public interface Item {
public String name();
public Packing packing();
public float price();
}
public interface Packing {
public String pack();
}
创建实现
Packking
接口的实现类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class Wrapper implements Packing {
public String pack() {
return "Wrapper";
}
}
public class Bottle implements Packing {
public String pack() {
return "Bottle";
}
}创建实现
Item
接口的抽象类,该类提供了默认的功能1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public abstract class Burger implements Item {
public Packing packing() {
return new Wrapper();
}
public abstract float price();
}
public abstract class ColdDrink implements Item {
public Packing packing() {
return new Bottle();
}
public abstract float price();
}创建扩展
Burger
和ColdDrink
的实体类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
51public class VegBurger extends Burger {
public float price() {
return 25.0f;
}
public String name() {
return "Veg Burger";
}
}
public class ChickenBurger extends Burger {
public float price() {
return 50.5f;
}
public String name() {
return "Chicken Burger";
}
}
public class Coke extends ColdDrink {
public float price() {
return 30.0f;
}
public String name() {
return "Coke";
}
}
public class Pepsi extends ColdDrink {
public float price() {
return 35.0f;
}
public String name() {
return "Pepsi";
}
}创建一个
Meal
类,带有上面定义的Item
对象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
26import java.util.ArrayList;
import java.util.List;
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());
}
}
}创建一个
MealBuilder
类,实际的builder
类负责创建Meal
对象1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public 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;
}
}BuiderPatternDemo
使用MealBuider
来演示建造者模式1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public 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());
}
}输出结果
1
2
3
4
5
6
7
8
9
10Veg Meal
Item : Veg Burger, Packing : Wrapper, Price : 25.0
Item : Coke, Packing : Bottle, Price : 30.0
Total Cost: 55.0
Non-Veg Meal
Item : Chicken Burger, Packing : Wrapper, Price : 50.5
Item : Pepsi, Packing : Bottle, Price : 35.0
Total Cost: 85.5
总结
将建造复杂对象的过程和组成对象的部件解耦