课程名:C#面向对象设计模式纵横谈(4)Builder 生成器模式(创建型模式) <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><personname w:st="on" productid="李"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-ascii-font-family: Verdana; mso-hansi-font-family: 'Times New Roman'">李</span></personname>老师先举了一个很有意思的例子来说明Builder的应用场景:假设创建游戏中的一个房屋House,该房屋由几个部分组成,且各个部分都会变化(比如说今天地板铺的是木地板,可明天却想换成瓷砖的)。如果使用最直观的设计方法,每一个房屋部分的变化,都将导致房屋构建的重新修正,那应该如何应对这种变化?

在软件系统中,有时候面临着一个复杂对象的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却相对稳定。如何应对这种变化?如何提供一种封装机制来隔离出复杂对象的各个部分的变化,从而保持系统中的稳定构建算法不随着需求改变而改变?

将一个复杂对象的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。

——《设计模式》GoF

结构图(Structure


WebCast听课录(6)

协作图(Collaborations

WebCast听课录(6)

示例代码:


WebCast听课录(6)


WebCast听课录(6)abstractclassHouse
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
WebCast听课录(6)}

WebCast听课录(6)
WebCast听课录(6)
publicabstractclassFullHouse:House
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
WebCast听课录(6)}

WebCast听课录(6)
publicabstractclassBuilder
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
publicabstractvoidBuildDoor();
WebCast听课录(6)
publicabstractvoidBuildWall();
WebCast听课录(6)
publicabstractvoidBuildWindow();
WebCast听课录(6)
publicabstractvoidBuildFloor();
WebCast听课录(6)
publicabstractvoidBuildCeiling();
WebCast听课录(6)
WebCast听课录(6)
publicabstractHouseGetHouse();
WebCast听课录(6)
WebCast听课录(6)}

WebCast听课录(6)
WebCast听课录(6)
publicclassFullHouseBuilder:Builder
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
publicoverridevoidBuildCeiling()
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
WebCast听课录(6)}

WebCast听课录(6)
WebCast听课录(6)
publicoverridevoidBuildDoor()
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
WebCast听课录(6)}

WebCast听课录(6)
WebCast听课录(6)
publicoverridevoidBuildFloor()
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
WebCast听课录(6)}

WebCast听课录(6)
WebCast听课录(6)
publicoverridevoidBuildWall()
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
WebCast听课录(6)}

WebCast听课录(6)
publicoverridevoidBuildWindow()
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
WebCast听课录(6)}

WebCast听课录(6)
publicoverrideHouseGetHouse()
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
returnnewFullHouse();
WebCast听课录(6)}

WebCast听课录(6)
WebCast听课录(6)}

WebCast听课录(6)
WebCast听课录(6)
publicclassGameManager
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
publicstaticHouseCreatHouse(Builderbuilder)
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)builder.BuildDoor();
WebCast听课录(6)builder.BuildFloor();
WebCast听课录(6)builder.BuildWall();
WebCast听课录(6)builder.BuildWindow();
WebCast听课录(6)
WebCast听课录(6)
returnbuilder.GetHouse();
WebCast听课录(6)}

WebCast听课录(6)}

WebCast听课录(6)
WebCast听课录(6)
classTest
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
WebCast听课录(6)
publicstaticvoidMain(String[]args)
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)
stringassemblyName=ConfigurationSettings["BuilderAssebmly"];
WebCast听课录(6)
stringbuilderName=ConfigurationSettings["BuilderClass"];
WebCast听课录(6)
WebCast听课录(6)Assemblyassembly
=Assembly.Load(assemblyName);
WebCast听课录(6)
WebCast听课录(6)Typet
=assembly.GetType("builderName");
WebCast听课录(6)
WebCast听课录(6)Builderbuilder
=Activator.CreateInstance(t);
WebCast听课录(6)
WebCast听课录(6)Househ
=GameManager.CreatHouse(builder);
WebCast听课录(6)}

WebCast听课录(6)}

WebCast听课录(6)

小结:

1Builder 模式主要用于分步骤构建一个复杂的对象。在这其中分步骤是一个稳定的算法,而复杂对象的各个部分则经常变化。

2,变化点在哪里,封装哪里—— Builder 模式主要在

于应对复杂对象各个部分的频繁需求变动。其缺点在于难以应对分步骤构建算法的需求变动。

3Abstract Factory 模式解决系列对象的需求变化,Builder模式解决对象部分的需求变化。Builder模式通常和Composite模式组合使用。

后记

课程要结束时,李老师讲到可以通过配置文件使代码的依赖性降低到最小,今天尝试着实现了下。


App.config
文件

WebCast听课录(6)<?xmlversion="1.0"encoding="utf-8"?>
WebCast听课录(6)
<configuration>
WebCast听课录(6)
WebCast听课录(6)
<appSettings>
WebCast听课录(6)
<addkey="FactoryName"value="ConsoleApplication6.BenzCarFactory"/>
WebCast听课录(6)
</appSettings>
WebCast听课录(6)
WebCast听课录(6)
</configuration>
WebCast听课录(6)

WebCast听课录(6)publicstaticvoidMain()
WebCast听课录(6)WebCast听课录(6)
WebCast听课录(6){
WebCast听课录(6)CarTestFrameWorkcf
=newCarTestFrameWork();
WebCast听课录(6)
WebCast听课录(6)StringfactoryName
=System.Configuration.ConfigurationSettings.AppSettings["FactoryName"].ToString();
WebCast听课录(6)Assemblyassembly
=Assembly.Load(Assembly.GetExecutingAssembly().GetName());
WebCast听课录(6)
WebCast听课录(6)Typet
=assembly.GetType(factoryName);
WebCast听课录(6)CarFactorycarFactory
=(CarFactory)Activator.CreateInstance(t);
WebCast听课录(6)cf.DoTest(carFactory);
WebCast听课录(6)
WebCast听课录(6)}

WebCast听课录(6)
WebCast听课录(6)

相关文章: