设计模式之C#实现(二)---Builder

设计模式之C#实现---Builder

上次我们学习了创建型模式中的AbstractFactory,这次我们将要介绍一个和它比较像的创建型模式Builder(至于关于Builder的详细内容您可以参考GOF的书,在这里不重复了。)。在GOF的书里Builder的目的是这样的:Separate the construction of a complex object from its representation so that the same construction process can create different representations.在我的程序设计中很难免会使用复杂的对象比如:车的组成、电脑的组成以及人在内。那么我们在创建电脑这个对象的时候我想我们需要一步一步的创建组成电脑的每一部分,先创建CPU对象、Memory对象、HardDisk对象等等。Builder就是这样一种模式用来一步一步的创建对象的每一部分。回忆一下AbstractFactory也是创建一族相关的对象,Builder也是创建一些相关的对象,两者之间的区别很微妙需要在以后的实践中细细体会。

既然文章叫设计模式之C#实现那么肯定少不了代码了,这次我想说的更清楚一些,我打算从如下两个方面来实现,首先我想要直接实现他的结构,也就是我们在下面的图中看到的那些类。接着我将用以个具体的例子或者书上的例子描述一下用来加深理解,希望我的描述可以帮助你更好的学习。

       从图上我们可以看出我们的Builder接口中有两个BuilderPart方法AB,以及一个GetResult方法用来返回创建的对象。将我们用ConcreteBuilder1ConcreteBuilder1实现接口的时候我们分别在其中加入了一个Private的对象,用来返回建立好的对象,在该实例的内部则是经过了两步才完成了Product对象的初始化。我们建立的Product是由一个Hashtable组成,可以添加和显示自己的每一个部分(就是Hashtable里面的每一个键/值)。好了不废话了看看下面的实现代码,在WinForm中调试通过,你可以参看本系列的AbstractFactory文章找到里面的相关表现对象(RichTextBox)。

       代码中有少量的注释是为了更好的理解。

using System;

 

namespace Builder_Me{

 

       using System.Collections;

 

       // specifies an abstract interface for creating parts of a Product object.

       //为创建对象的一个部分指定一个接口

       public interface Builder{

              void BuildPartA();

              void BuildPartB();

              Product GetResult();

       }

 

       // constructs and assembles parts of the product by impementing the Builder interface.

       // defines and keeps track of the representation it creates.

       // provides an interface for retrieving the product.

       public class ConcreteBuilder1 : Builder{

              private Product m_Product;

              public void BuildPartA(){

                     this.m_Product = new Product();

                     this.m_Product.AddParts("1","PartA");

              }

              public void BuildPartB(){

                     this.m_Product.AddParts("2","PartB");

              }

 

              public Product GetResult(){

                     return this.m_Product;

              }

       }

 

       public class ConcreteBuilder2 : Builder{

              private Product m_Product;

 

              public void BuildPartA(){

                     //必须先调用该方法否则不能实例化对象

                     this.m_Product = new Product();

                     this.m_Product.AddParts("3","Part1");

              }

 

              public void BuildPartB(){

                     this.m_Product.AddParts("4","Part2");

              }

 

              public Product GetResult(){

                     return this.m_Product;

              }

       }

 

       // construct an object using the Builder interface.

       public class Director{

              public void Construct(Builder builder){

                     //顺序不能变

                     builder.BuildPartA();

                     builder.BuildPartB();

              }

       }

 

       // represents the complex object under construction.ConcreteBuilder builds

       // the product's internal representation and defines the process by which it's

       // assembled.

       // includes classes that define the constituent parts,including interfaces for

       // assembling the parts into the final result.

       //要创建复杂的对象该对象我们用Hashtable组合表示。

       public class Product{

              Hashtable m_Parts = new Hashtable();

 

              public void AddParts(string partKey,string partValue){

                     this.m_Parts.Add(partKey,partValue);

              }

 

              public string ShowSelfParts(){

                     string strResult = string.Empty;

                     int i = 1;

                     foreach(string strTmp in this.m_Parts.Values){

                            strResult +="Part"+i.ToString()+":/t"+strTmp+"/n";

                            i++;

                     }

                     return strResult;

              }

       }

}

 

客户端的代码片断如下:

Director director = new Director();

                     Builder builder1 = new ConcreteBuilder1();

                     Builder builder2 = new ConcreteBuilder2();

                     director.Construct( builder1 );

                     Product p1 = builder1.GetResult();

                     this.richTextBox1.AppendText(p1.ShowSelfParts());

 

                     director.Construct( builder2 );

                     Product p2 = builder2.GetResult();

                     this.richTextBox1.AppendText(p2.ShowSelfParts());

由于GOF的例子是C++实现所以转换成C#也非常容易,我在这里就不转换了,有兴趣的人可以转换跟帖。

本人能力有限,如果在上面有什么说错的或者不准确地方请网友指正,我将虚心学习,我的email:wu_jian830@hotmail.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Builder模式 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示 统设计中,有时候面临着一个“复杂系统”的创建工作,该对象通常由各个部分的子对象用一定的算法构成,或者说按一定的步骤组合而成;这些的算法和步骤是稳定的,而构成这个对象的子对象却经常由于需求改变而发生变化,此时我们可以选择使用Builder模式。尽管Builder模式相当容易让人误解,但我认为这几点应该是不会有错的。因此我个人认为Builder模式中要好好把握如下几点 1. 需要创建的对象是复杂对象 2. 对象的创建过程是一步步创建(即:可能由于过程的不同而导致有不同的对象展示) 3. 明确建造者(Builder)、具体建造者(Concrete Builder)、指导者(Director)、产品(Product)之间的职责和联系。 ◆建造者(Builder): 给出一个抽象接口,以规范产品对象的各个组成成分的建造。一般而言,此接口独立于应用程序的商业逻辑。模式中直接创建产品对象的是 具体建造者(ConcreteBuilder):具体建造者类必须实现这个接口所要求的方法:一个是建造方法,另一个是结果返还方法。 ◆具体建造者(Concrete Builder): 担任这个角色的是于应用程序紧密相关的类,它们在应用程序调用下创建产品实例。这个角色主要完成的任务包括: ■实现Builder角色提供的接口,一步一步完成创建产品实例的过程。 ■在建造过程完成后,提供产品的实例。 ◆指导者(Director): 担任这个角色的类调用具体建造者角色以创建产品对象。导演者并没有产品类的具体知识,真正拥有产品类的具体知识的是具体建造者对象。 ◆产品(Product): 产品便是建造中的复杂对象。 详细见博客 http://blog.csdn.net/xiaoting451292510/article/details/8330462

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值