建造者模式(Builder Pattern)
——.NET设计模式系列之四
Terrylee,2005年12月17日
概述
在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法确相对稳定。如何应对这种变化?如何提供一种“封装机制”来隔离出“复杂对象的各个部分”的变化,从而保持系统中的“稳定构建算法”不随着需求改变而改变?这就是要说的建造者模式。
本文通过现实生活中的买KFC的例子,用图解的方式来诠释建造者模式。
意图
将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
模型图
![第3章 建造者模式(Builder Pattern)](http://img.e-com-net.com/image/product/568d2ad8b7f84e7d9a7fa35dea5ff2c9.jpg)
生活中的例子
生成器模式将复杂对象的构建与对象的表现分离开来,这样使得同样的构建过程可以创建出不同的表现。这种模式用于快餐店制作儿童餐。典型的儿童餐包括一个主食,一个辅食,一杯饮料和一个玩具(例如汉堡、炸鸡、可乐和玩具车)。这些在不同的儿童餐中可以是不同的,但是组合成儿童餐的过程是相同的。无论顾客点的是汉堡,三名治还是鸡肉,过程都是一样的。柜台的员工直接把主食,辅食和玩具放在一起。这些是放在一个袋子中的。饮料被倒入杯中,放在袋子外边。这些过程在相互竞争的餐馆中是同样的。
![第3章 建造者模式(Builder Pattern)](http://img.e-com-net.com/image/product/c59a9b519633477fb1a7714ec7426112.jpg)
实现过程图解
在这里我们还是以去KFC店买套餐为例子,示意图如下:
![第3章 建造者模式(Builder Pattern)](http://img.e-com-net.com/image/product/0a4f1100f3d24da58c0da5df2184c453.jpg)
客户端:顾客。想去买一套套餐(这里面包括汉堡,可乐,薯条),可以有1号和2号两种套餐供顾客选择。
指导者角色:收银员。知道顾客想要买什么样的套餐,并告诉餐馆员工去准备套餐。
建造者角色:餐馆员工。按照收银员的要求去准备具体的套餐,分别放入汉堡,可乐,薯条等。
产品角色:最后的套餐,所有的东西放在同一个盘子里面。
下面开始我们的买套餐过程。
1.客户创建Derector对象,并用它所想要的Builder对象进行配置。顾客进入KFC店要买套餐,先找到一个收银员,相当于创建了一个指导者对象。这位收银员给出两种套餐供顾客选择:1普通套餐,2黄金套餐。完成的工作如时序图中红色部分所示。
![第3章 建造者模式(Builder Pattern)](http://img.e-com-net.com/image/product/196531d047b34f1ead4b56a89238d801.jpg)
程序实现:
1
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem;
2
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem.Configuration;
3
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem.Reflection;
4
5
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
namespaceKFC
6
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
7
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
8
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///Client类
9
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///</summary>
10
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicclassClient
11
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
12
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicstaticvoidMain(string[]args)
13
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
14
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
FoodManagerfoodmanager=newFoodManager();
15
16
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Builderinstance;
17
18
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Console.WriteLine("PleaseEnterFoodNo:");
19
20
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
stringNo=Console.ReadLine();
21
22
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
stringfoodType=ConfigurationSettings.AppSettings["No"+No];
23
24
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
instance=(Builder)Assembly.Load("KFC").CreateInstance("KFC."+foodType);
25
26
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
foodmanager.Construct(instance);
27
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
28
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
29
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
30
产品(套餐)类:
1
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem;
2
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem.Collections;
3
4
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
namespaceKFC
5
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
6
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
7
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///Food类,即产品类
8
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///</summary>
9
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicclassFood
10
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
11
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Hashtablefood=newHashtable();
12
13
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
14
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///添加食品
15
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///</summary>
16
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///<paramname="strName">食品名称</param>
17
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///<paramname="Price">价格</param>
18
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicvoidAdd(stringstrName,stringPrice)
19
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
20
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
food.Add(strName,Price);
21
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
22
23
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
24
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///显示食品清单
25
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///</summary>
26
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicvoidShow()
27
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
28
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
IDictionaryEnumeratormyEnumerator=food.GetEnumerator();
29
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Console.WriteLine("FoodList:");
30
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Console.WriteLine("------------------------------");
31
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
stringstrfoodlist="";
32
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
while(myEnumerator.MoveNext())
33
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
34
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
strfoodlist=strfoodlist+"\n\n"+myEnumerator.Key.ToString();
35
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
strfoodlist=strfoodlist+":\t"+myEnumerator.Value.ToString();
36
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
37
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Console.WriteLine(strfoodlist);
38
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Console.WriteLine("\n------------------------------");
39
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
40
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
41
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
42
2.指导者通知建造器。收银员(指导者)告知餐馆员工准备套餐。这里我们准备套餐的顺序是:放入汉堡,可乐倒入杯中,薯条放入盒中,并把这些东西都放在盘子上。这个过程对于普通套餐和黄金套餐来说都是一样的,不同的是它们的汉堡,可乐,薯条价格不同而已。如时序图红色部分所示:
![第3章 建造者模式(Builder Pattern)](http://img.e-com-net.com/image/product/f7b8bc6eca0741fc84fbfc5409cd698b.jpg)
程序实现:
1
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem;
2
3
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
namespaceKFC
4
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
5
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
6
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///FoodManager类,即指导者
7
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///</summary>
8
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicclassFoodManager
9
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
10
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicvoidConstruct(Builderbuilder)
11
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
12
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
builder.BuildHamb();
13
14
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
builder.BuildCoke();
15
16
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
builder.BuildChip();
17
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
18
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
19
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
20
3.建造者处理指导者的要求,并将部件添加到产品中。餐馆员工(建造者)按照收银员要求的把对应的汉堡,可乐,薯条放入盘子中。这部分是建造者模式里面富于变化的部分,因为顾客选择的套餐不同,套餐的组装过程也不同,这步完成产品对象的创建工作。
程序实现:
1
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem;
2
3
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
namespaceKFC
4
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
5
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
6
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///Builder类,即抽象建造者类,构造套餐
7
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///</summary>
8
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicabstractclassBuilder
9
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
10
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
11
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///添加汉堡
12
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///</summary>
13
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicabstractvoidBuildHamb();
14
15
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
16
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///添加可乐
17
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///</summary>
18
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicabstractvoidBuildCoke();
19
20
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
21
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///添加薯条
22
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///</summary>
23
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicabstractvoidBuildChip();
24
25
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
26
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///返回结果
27
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///</summary>
28
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///<returns>食品对象</returns>
29
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicabstractFoodGetFood();
30
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
31
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
32
1
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem;
2
3
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
namespaceKFC
4
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
5
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
6
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///NormalBuilder类,具体构造者,普通套餐
7
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///</summary>
8
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicclassNormalBuilder:Builder
9
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
10
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
privateFoodNormalFood=newFood();
11
12
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicoverridevoidBuildHamb()
13
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
14
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
NormalFood.Add("NormalHamb","¥10.50");
15
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
16
17
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicoverridevoidBuildCoke()
18
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
19
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
NormalFood.Add("CokeCole","¥4.50");
20
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
21
22
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicoverridevoidBuildChip()
23
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
24
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
NormalFood.Add("FireChips","¥2.00");
25
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
26
27
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicoverrideFoodGetFood()
28
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
29
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
returnNormalFood;
30
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
31
32
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
33
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
34
1
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem;
2
3
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
namespaceKFC
4
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
5
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
6
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///GoldBuilder类,具体构造者,黄金套餐
7
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///</summary>
8
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicclassGoldBuilder:Builder
9
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
10
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
privateFoodGoldFood=newFood();
11
12
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicoverridevoidBuildHamb()
13
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
14
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
GoldFood.Add("GoldHamb","¥13.50");
15
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
16
17
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicoverridevoidBuildCoke()
18
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
19
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
GoldFood.Add("CokeCole","¥4.50");
20
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
21
22
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicoverridevoidBuildChip()
23
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
24
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
GoldFood.Add("FireChips","¥3.50");
25
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
26
27
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicoverrideFoodGetFood()
28
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
29
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
returnGoldFood;
30
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
31
32
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
33
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
34
4.客户从建造者检索产品。从餐馆员工准备好套餐后,顾客再从餐馆员工那儿拿回套餐。这步客户程序要做的仅仅是取回已经生成的产品对象,如时序图中红色部分所示。
![第3章 建造者模式(Builder Pattern)](http://img.e-com-net.com/image/product/4b9526b4913041e1a0f706eff76c0e39.jpg)
完整的客户程序:
1
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem;
2
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem.Configuration;
3
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem.Reflection;
4
5
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
namespaceKFC
6
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
7
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
/**////<summary>
8
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///Client类
9
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
///</summary>
10
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicclassClient
11
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
12
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicstaticvoidMain(string[]args)
13
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
14
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
FoodManagerfoodmanager=newFoodManager();
15
16
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Builderinstance;
17
18
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Console.WriteLine("PleaseEnterFoodNo:");
19
20
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
stringNo=Console.ReadLine();
21
22
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
stringfoodType=ConfigurationSettings.AppSettings["No"+No];
23
24
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
instance=(Builder)Assembly.Load("KFC").CreateInstance("KFC."+foodType);
25
26
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
foodmanager.Construct(instance);
27
28
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Foodfood=instance.GetFood();
29
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
food.Show();
30
31
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Console.ReadLine();
32
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
33
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
34
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
35
通过分析不难看出,在这个例子中,在准备套餐的过程是稳定的,即按照一定的步骤去做,而套餐的组成部分则是变化的,有可能是普通套餐或黄金套餐等。这个变化就是建造者模式中的“变化点“,就是我们要封装的部分。
另外一个例子
在这里我们再给出另外一个关于建造房子的例子。客户程序通过调用指导者 (CDirector class)的BuildHouse()方法来创建一个房子。该方法有一个布尔型的参数blnBackyard,当blnBackyard为假时指导者将创建一个Apartment(Concrete Builder),当它为真时将创建一个Single Family Home(Concrete Builder)。这两种房子都实现了接口Ihouse。
程序实现:
1
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
//关于建造房屋的例子
2
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem;
3
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
usingSystem.Collections;
4
5
![](http://img.e-com-net.com/image/product/1ed4d00dbefa46d4827192b56e5fca8d.gif)
/**////<summary>
6
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///抽象建造者
7
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
///</summary>
8
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
publicinterfaceIHouse
9
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
10
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
boolGetBackyard();
11
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
longNoOfRooms();
12
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
stringDescription();
13
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
14
15
![](http://img.e-com-net.com/image/product/1ed4d00dbefa46d4827192b56e5fca8d.gif)
/**////<summary>
16
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///具体建造者
17
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
///</summary>
18
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
publicclassCApt:IHouse
19
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
20
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
privateboolmblnBackyard;
21
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
privateHashtableRooms;
22
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicCApt()
23
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
24
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
CRoomroom;
25
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Rooms=newHashtable();
26
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room=newCRoom();
27
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room.RoomName="MasterBedroom";
28
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Rooms.Add("room1",room);
29
30
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room=newCRoom();
31
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room.RoomName="SecondBedroom";
32
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Rooms.Add("room2",room);
33
34
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room=newCRoom();
35
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room.RoomName="LivingRoom";
36
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Rooms.Add("room3",room);
37
38
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
mblnBackyard=false;
39
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
40
41
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicboolGetBackyard()
42
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
43
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
returnmblnBackyard;
44
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
45
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publiclongNoOfRooms()
46
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
47
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
returnRooms.Count;
48
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
49
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicstringDescription()
50
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
51
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
IDictionaryEnumeratormyEnumerator=Rooms.GetEnumerator();
52
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
stringstrDescription;
53
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
strDescription="ThisisanApartmentwith"+Rooms.Count+"Rooms\n";
54
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
strDescription=strDescription+"ThisApartmentdoesn'thaveabackyard\n";
55
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
while(myEnumerator.MoveNext())
56
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
57
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
strDescription=strDescription+"\n"+myEnumerator.Key+"\t"+((CRoom)myEnumerator.Value).RoomName;
58
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
59
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
returnstrDescription;
60
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
61
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
62
63
![](http://img.e-com-net.com/image/product/1ed4d00dbefa46d4827192b56e5fca8d.gif)
/**////<summary>
64
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///具体建造者
65
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
///</summary>
66
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
publicclassCSFH:IHouse
67
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
68
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
privateboolmblnBackyard;
69
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
privateHashtableRooms;
70
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicCSFH()
71
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
72
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
CRoomroom;
73
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Rooms=newHashtable();
74
75
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room=newCRoom();
76
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room.RoomName="MasterBedroom";
77
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Rooms.Add("room1",room);
78
79
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room=newCRoom();
80
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room.RoomName="SecondBedroom";
81
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Rooms.Add("room2",room);
82
83
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room=newCRoom();
84
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room.RoomName="ThirdRoom";
85
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Rooms.Add("room3",room);
86
87
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room=newCRoom();
88
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room.RoomName="LivingRoom";
89
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Rooms.Add("room4",room);
90
91
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room=newCRoom();
92
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
room.RoomName="GuestRoom";
93
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Rooms.Add("room5",room);
94
95
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
mblnBackyard=true;
96
97
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
98
99
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicboolGetBackyard()
100
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
101
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
returnmblnBackyard;
102
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
103
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publiclongNoOfRooms()
104
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
105
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
returnRooms.Count;
106
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
107
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicstringDescription()
108
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
109
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
IDictionaryEnumeratormyEnumerator=Rooms.GetEnumerator();
110
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
stringstrDescription;
111
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
strDescription="ThisisanSingleFamilyHomewith"+Rooms.Count+"Rooms\n";
112
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
strDescription=strDescription+"Thishousehasabackyard\n";
113
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
while(myEnumerator.MoveNext())
114
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
115
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
strDescription=strDescription+"\n"+myEnumerator.Key+"\t"+((CRoom)myEnumerator.Value).RoomName;
116
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
117
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
returnstrDescription;
118
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
119
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
120
121
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
publicinterfaceIRoom
122
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
123
![](http://img.e-com-net.com/image/product/a1a87b341f6d4570945852fd86f0d48f.gif)
stringRoomName
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{get;set;}
124
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
125
126
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
publicclassCRoom:IRoom
127
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
128
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
privatestringmstrRoomName;
129
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicstringRoomName
130
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
131
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
get
132
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
133
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
returnmstrRoomName;
134
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
135
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
set
136
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
137
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
mstrRoomName=value;
138
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
139
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
140
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
141
142
![](http://img.e-com-net.com/image/product/1ed4d00dbefa46d4827192b56e5fca8d.gif)
/**////<summary>
143
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///指导者
144
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
///</summary>
145
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
publicclassCDirector
146
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
147
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicIHouseBuildHouse(boolblnBackyard)
148
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
149
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
if(blnBackyard)
150
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
151
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
returnnewCSFH();
152
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
153
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
else
154
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
155
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
returnnewCApt();
156
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
157
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
158
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
159
160
![](http://img.e-com-net.com/image/product/1ed4d00dbefa46d4827192b56e5fca8d.gif)
/**////<summary>
161
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
///客户程序
162
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
///</summary>
163
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
publicclassClient
164
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
165
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
staticvoidMain(string[]args)
166
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
167
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
CDirectorobjDirector=newCDirector();
168
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
IHouseobjHouse;
169
170
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
stringInput=Console.ReadLine();
171
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
objHouse=objDirector.BuildHouse(bool.Parse(Input));
172
173
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Console.WriteLine(objHouse.Description());
174
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Console.ReadLine();
175
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
176
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
177
178
建造者模式的几种演化
省略抽象建造者角色
系统中只需要一个具体建造者,省略掉抽象建造者,结构图如下:
![第3章 建造者模式(Builder Pattern)](http://img.e-com-net.com/image/product/8d01cd6ff7b340f8b4bcbc5596637c36.jpg)
指导者代码如下:
1
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
classDirector
2
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
3
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
privateConcreteBuilderbuilder;
4
5
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicvoidConstruct()
6
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
7
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
builder.BuildPartA();
8
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
builder.BuildPartB();
9
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
10
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
省略指导者角色
抽象建造者角色已经被省略掉,还可以省略掉指导者角色。让Builder角色自己扮演指导者与建造者双重角色。结构图如下:
![第3章 建造者模式(Builder Pattern)](http://img.e-com-net.com/image/product/e1122c153f934a548d08b0d6eb9dd87f.jpg)
建造者角色代码如下:
1
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
publicclassBuilder
2
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
3
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
privateProductproduct=newProduct();
4
5
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicvoidBuildPartA()
6
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
7
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
//
8
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
9
10
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicvoidBuildPartB()
11
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
12
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
//
13
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
14
15
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicProductGetResult()
16
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
17
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
returnproduct;
18
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
19
20
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicvoidConstruct()
21
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
22
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
BuildPartA();
23
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
BuildPartB();
24
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
25
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
客户程序:
1
![](http://img.e-com-net.com/image/product/5fe150ee7c4649e99cae25e6b7c75c1f.gif)
publicclassClient
2
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
3
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
privatestaticBuilderbuilder;
4
5
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
publicstaticvoidMain()
6
![](http://img.e-com-net.com/image/product/d55068b7eba0455eba2e81fccabd26f6.png)
{
7
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
builder=newBuilder();
8
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
builder.Construct();
9
![](http://img.e-com-net.com/image/product/6a2e176ed41042258298fe95142b9240.gif)
Productproduct=builder.GetResult();
10
![](http://img.e-com-net.com/image/product/e6b92b2d20424855bab0299b6a87f3e7.gif)
}
11
![](http://img.e-com-net.com/image/product/e48f65910e6f4b7087d4ea97c99bda88.gif)
}
合并建造者角色和产品角色
建造模式失去抽象建造者角色和指导者角色后,可以进一步退化,从而失去具体建造者角色,此时具体建造者角色和产品角色合并,从而使得产品自己就是自己的建造者。这样做混淆了对象的建造者和对象本身,但是有时候一个产品对象有着固定的几个零件,而且永远只有这几个零件,此时将产品类和建造类合并,可以使系统简单易读。结构图如下:
![第3章 建造者模式(Builder Pattern)](http://img.e-com-net.com/image/product/e84c4f0b1e0b4d2789c377f9d61e7e9f.jpg)
实现要点
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、 ……
总结
建造者模式的实质是解耦组装过程和创建具体部件,使得我们不用去关心每个部件是如何组装的。
______________________________________________________________________________________
源程序下载:/Files/Terrylee/BuilderPattern.rar
参考资料:
《Java与设计模式》阎宏 著
《设计模式(中文版)》
《DesignPatternsExplained》