什么建造者模式(Builder Pattern)?
流程稳定,实现复杂,将实现过程封装隔离的设计模式.
包含对象 操纵(Client) 指导者(Director) 创建者(Builder) 产出者(Product)
如下图:
通过上图的分析大致 他们的关系就显而易见了.
下面一个小例子就能说明 这个关系
公司小王想要加薪 所以就给领导张总提出了加薪的要求,张总看小王的表现让财务小陈上添加50000元的薪水,小王高高兴兴的多得到了50000的薪水
这个个过程就可以用建造者的设计模式来解释.
操纵者发起人是 小王(上图中的Client)
指导者是 张总(上图中的Director)
执行者是 财务小陈 (上图中的Builder)
产品是 50000的薪水(上图中的Product)
满足这个设计模式的条件了吗?
执行过程相对稳定-->加薪的过程就是这样 提出申请 领导点头 财务操作
实现复杂-->提出申请 领导根据申请人的实际情况,工作现状 做出不同的态度和反应,如:加薪1块 或者是1000000块
另:
用户去买KFC的过程:
客户端:顾客。想去买一套套餐(这里面包括汉堡,可乐,薯条),可以有1号和2号两种套餐供顾客选择。
指导者角色:收银员。知道顾客想要买什么样的套餐,并告诉餐馆员工去准备套餐。
建造者角色:餐馆员工。按照收银员的要求去准备具体的套餐,分别放入汉堡,可乐,薯条等。
产品角色:最后的套餐,所有的东西放在同一个盘子里面。
以上例子也是同一个道理
实现代码:
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
30
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
产品(套餐)类:(该类中的ADD方法个人认为不太正确,add方法应该放到Builder类中,这是Builder对象的一个行为,但此处还是按照原作者的实现方式 将add放到了food中...同样show方法也该放懂啊FoodManager中,food 就应该是一个实体,放的是价格名称什么的)
1using System;
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
42
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2.指导者通知建造器。收银员(指导者)告知餐馆员工准备套餐。这里我们准备套餐的顺序是:放入汉堡,可乐倒入杯中,薯条放入盒中,并把这些东西都放在盘子上。这个过程对于普通套餐和黄金套餐来说都是一样的,不同的是它们的汉堡,可乐,薯条价格不同而已。如时序图红色部分所示:
程序实现:
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
3.建造者处理指导者的要求,并将部件添加到产品中。餐馆员工(建造者)按照收银员要求的把对应的汉堡,可乐,薯条放入盘子中。这部分是建造者模式里面富于变化的部分,因为顾客选择的套餐不同,套餐的组装过程也不同,这步完成产品对象的创建工作。
程序实现:
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
32
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
34
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
34
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4.客户从建造者检索产品。从餐馆员工准备好套餐后,顾客再从餐馆员工那儿拿回套餐。这步客户程序要做的仅仅是取回已经生成的产品对象,如时序图中红色部分所示。
完整的客户程序:
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
35
![](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
通过分析不难看出,在这个例子中,在准备套餐的过程是稳定的,即按照一定的步骤去做,而套餐的组成部分则是变化的,有可能是普通套餐或黄金套餐等。这个变化就是建造者模式中的“变化点“,就是我们要封装的部分。
实现要点
1、建造者模式主要用于“分步骤构建一个复杂的对象”,在这其中“分步骤”是一个稳定的算法,而复杂对象的各个部分则经常变化。
2、产品不需要抽象类,特别是由于创建对象的算法复杂而导致使用此模式的情况下或者此模式应用于产品的生成过程,其最终结果可能差异很大,不大可能提炼出一个抽象产品类。
3、创建者中的创建子部件的接口方法不是抽象方法而是空方法,不进行任何操作,具体的创建者只需要覆盖需要的方法就可以,但是这也不是绝对的,特别是类似文本转换这种情况下,缺省的方法将输入原封不动的输出是合理的缺省操作。
4、前面我们说过的抽象工厂模式(Abtract Factory)解决“系列对象”的需求变化,Builder模式解决“对象部分”的需求变化,建造者模式常和组合模式(Composite Pattern)结合使用。
效果
1、建造者模式的使用使得产品的内部表象可以独立的变化。使用建造者模式可以使客户端不必知道产品内部组成的细节。
2、每一个Builder都相对独立,而与其它的Builder无关。
3、可使对构造过程更加精细控制。
4、将构建代码和表示代码分开。
5、建造者模式的缺点在于难于应付“分步骤构建算法”的需求变动。
适用性
以下情况应当使用建造者模式:
1、需要生成的产品对象有复杂的内部结构。
2、需要生成的产品对象的属性相互依赖,建造者模式可以强迫生成顺序。
3、 在对象创建过程中会使用到系统中的一些其它对象,这些对象在产品对象的创建过程中不易得到。
应用场景
1、 RTF文档交换格式阅读器。
2、 .NET环境下的字符串处理StringBuilder,这是一种简化了的建造者模式。
3、 ……
总结
建造者模式的实质是解耦组装过程和创建具体部件,使得我们不用去关心每个部件是如何组装的。