作者:
cuike519的专栏 http://blog.csdn.net/cuike519/我们将要介绍一个和它比较像的创建型模式
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#实现那么肯定少不了代码了,这次我想说的更清楚一些,我打算从如下两个方面来实现,首先我想要直接实现他的结构,也就是我们在下面的图中看到的那些类。接着我将用以个具体的例子或者书上的例子描述一下用来加深理解,希望我的描述可以帮助你更好的学习。
<!----><v:shapetype o:spt="75" coordsize="21600,21600" filled="f" stroked="f" id="_x0000_t75" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" o:connecttype="rect" gradientshapeok="t"></v:path><!----><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype>
从图上我们可以看出我们的Builder接口中有两个BuilderPart方法A、B,以及一个GetResult方法用来返回创建的对象。将我们用ConcreteBuilder1和ConcreteBuilder1实现接口的时候我们分别在其中加入了一个Private的对象,用来返回建立好的对象,在该实例的内部则是经过了两步才完成了Product对象的初始化。我们建立的Product是由一个Hashtable组成,可以添加和显示自己的每一个部分(就是Hashtable里面的每一个键/值)。好了不废话了看看下面的实现代码,在WinForm中调试通过,你可以参看本系列的AbstractFactory文章找到里面的相关表现对象(RichTextBox)。
代码中有少量的注释是为了更好的理解。
using System;<o:p></o:p>
<o:p> </o:p>
namespace Builder_Me{<o:p></o:p>
<o:p> </o:p>
using System.Collections;<o:p></o:p>
<o:p> </o:p>
// specifies an abstract interface for creating parts of a Product object.<o:p></o:p>
//为创建对象的一个部分指定一个接口<o:p></o:p>
public interface Builder{<o:p></o:p>
void BuildPartA();<o:p></o:p>
void BuildPartB();<o:p></o:p>
Product GetResult();<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
// constructs and assembles parts of the product by impementing the Builder interface.<o:p></o:p>
// defines and keeps track of the representation it creates.<o:p></o:p>
// provides an interface for retrieving the product.<o:p></o:p>
public class ConcreteBuilder1 : Builder{<o:p></o:p>
private Product m_Product;<o:p></o:p>
public void BuildPartA(){<o:p></o:p>
this.m_Product = new Product();<o:p></o:p>
this.m_Product.AddParts("1","PartA");<o:p></o:p>
}<o:p></o:p>
public void BuildPartB(){<o:p></o:p>
this.m_Product.AddParts("2","PartB");<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
public Product GetResult(){<o:p></o:p>
return this.m_Product;<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
public class ConcreteBuilder2 : Builder{<o:p></o:p>
private Product m_Product;<o:p></o:p>
<o:p> </o:p>
public void BuildPartA(){<o:p></o:p>
//必须先调用该方法否则不能实例化对象<o:p></o:p>
this.m_Product = new Product();<o:p></o:p>
this.m_Product.AddParts("3","Part1");<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
public void BuildPartB(){<o:p></o:p>
this.m_Product.AddParts("4","Part2");<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
public Product GetResult(){<o:p></o:p>
return this.m_Product;<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
// construct an object using the Builder interface.<o:p></o:p>
public class Director{<o:p></o:p>
public void Construct(Builder builder){<o:p></o:p>
//顺序不能变<o:p></o:p>
builder.BuildPartA();<o:p></o:p>
builder.BuildPartB();<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
// represents the complex object under construction.ConcreteBuilder builds<o:p></o:p>
// the product's internal representation and defines the process by which it's<o:p></o:p>
// assembled.<o:p></o:p>
// includes classes that define the constituent parts,including interfaces for<o:p></o:p>
// assembling the parts into the final result.<o:p></o:p>
//要创建复杂的对象该对象我们用Hashtable组合表示。<o:p></o:p>
public class Product{<o:p></o:p>
Hashtable m_Parts = new Hashtable();<o:p></o:p>
<o:p> </o:p>
public void AddParts(string partKey,string partValue){<o:p></o:p>
this.m_Parts.Add(partKey,partValue);<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
public string ShowSelfParts(){<o:p></o:p>
string strResult = string.Empty;<o:p></o:p>
int i = 1;<o:p></o:p>
foreach(string strTmp in this.m_Parts.Values){<o:p></o:p>
strResult +="Part"+i.ToString()+":\t"+strTmp+"\n";<o:p></o:p>
i++;<o:p></o:p>
}<o:p></o:p>
return strResult;<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
<o:p> </o:p>
客户端的代码片断如下:
Director director = new Director();<o:p></o:p>
Builder builder1 = new ConcreteBuilder1();<o:p></o:p>
Builder builder2 = new ConcreteBuilder2();<o:p></o:p>
director.Construct( builder1 );<o:p></o:p>
Product p1 = builder1.GetResult();<o:p></o:p>
this.richTextBox1.AppendText(p1.ShowSelfParts());<o:p></o:p>
<o:p> </o:p>
director.Construct( builder2 );<o:p></o:p>
Product p2 = builder2.GetResult();<o:p></o:p>
this.richTextBox1.AppendText(p2.ShowSelfParts());<o:p></o:p>
由于GOF的例子是C++实现所以转换成C#也非常容易,我在这里就不转换了,有兴趣的人可以转换跟帖。<o:p></o:p>
本人能力有限,如果在上面有什么说错的或者不准确地方请网友指正,我将虚心学习,我的email:wu_jian830@hotmail.com。<o:p></o:p>
分享到:
相关推荐
C#设计模式(8)-Builder Pattern C#设计模式(7)-Singleton Pattern C#设计模式(6)-Abstract Factory Pattern C#设计模式(5)-Factory Method Pattern C#设计模式(4)-Simple Factory Pattern C#设计模式...
C#设计模式(8)-Builder Pattern 57 一、 建造者(Builder)模式 57 二、 Builder模式的结构: 58 三、 程序举例: 58 四、 建造者模式的活动序列: 62 五、 建造者模式的实现: 62 六、 建造者模式的演化 68 七、...
C#设计模式(8)-Builder Pattern 一、 建造者(Builder)模式 二、 Builder模式的结构: 三、 程序举例: 四、 建造者模式的活动序列: 五、 建造者模式的实现: 六、 建造者模式的演化 七、 在什么情况下...
C#视频-面向对象设计模式纵横谈(4):Builder 生成器模式(创建型模式)
C#设计模式之建造者(Builder)模式示例源代码
在这里与各位分享本人从网络上下载的C#面向对象设计模式纵横谈系列视频,共有25节,除了第一节需要各位贡献一点资源分以作为对本人上传资源的回馈,后面的其他资源均不需要资源分。敬请期待。 这是第4节:创建型...
C#面向对象设计模式纵横谈(4):Builder 生成器模式(创建型模式) C#面向对象设计模式纵横谈(5):Factory Method 工厂方法模式(创建型模式) C#面向对象设计模式纵横谈(6):Prototype 原型模式(创建型模式) C#面向...
将一个复杂对象的构造与它的表示分离,使同样的构建 过程可以创建不同的表示,这样的设计模式被称为建造者模式。
建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype)结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite ...
C#面向对象设计模式纵横谈(4):Builder 生成器模式(创建型模式) 体验课程
23种设计模式(C#实现)演示 1.预备篇 UML类图10分钟快速入门 2.创造型模式 ①设计模式的征途-01。单例(Singleton)模式 ②设计模式的征途-02。简单工厂(Simple Factory)模式 ③设计模式的征途-03。工厂方法...
C#设计模式(23种设计模式) 部分内容概述如下,下载可看全部哦!!! 还等什么呢?? 创建型: 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式...
设计模式之我见(1):设计模式概述 (100%) 设计模式面面观(2):设计模式基本要素与原则 (100%) 设计模式面面观(3):单件模式(Singletion)-创建型模式 (100%) 设计模式面面观(4):工厂模式...
多 迭代子模式 个对象聚在一起形成的总体称之为聚集, 聚集对象是能够包容一组对象的容器对象。 迭代 子 模式将迭代逻辑封装到一个独立的子对象中, 从而与聚集本身隔开。 迭代子模式简化了聚集 的界面。每一个聚集...
工厂方法模式(Factory Method Pattern) 观察者模式(Observer Pattern) 建造者模式(Builder Pattern) 解释器模式(Interpreter Pattern) 命令模式(Command Pattern) 模板方法模式(Template Method Pattern) 桥接模式...
C#设计模式(23种设计模式) 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype) 结构型: 6. 适配器...
│ │ └─C#设计模式(6)——原型模式(Prototype Patt O技术博客_files │ └─PrototypePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─07.Adapter...
文章地址:https://blog.csdn.net/mazhiyuan1981/article/details/110638833
C#面向对象设计模式纵横谈(4):Builder 生成器模式(创建型模式) (Level 300)
CSharp --AbstractFactory --Adapter --Bridge --Builder --ChainOfResponsibility