设计模式之C#实现---Builder
我們將要介紹一個和它比較像的創建型模式 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 就是這樣一種模式用來一步一步的創建對象的每一部分?;貞浺幌?/span> AbstractFactory 也是創建一族相關的對象, Builder 也是創建一些相關的對象,兩者之間的區別很微妙需要在以后的實踐中細細體會。
既然文章叫設計模式之C#實現那么肯定少不了代碼了,這次我想說的更清楚一些,我打算從如下兩個方面來實現,首先我想要直接實現他的結構,也就是我們在下面的圖中看到的那些類。接著我將用以個具體的例子或者書上的例子描述一下用來加深理解,希望我的描述可以幫助你更好的學習。
?????? 從圖上我們可以看出我們的Builder接口中有兩個BuilderPart方法A、B,以及一個GetResult方法用來返回創建的對象。將我們用ConcreteBuilder1和ConcreteBuilder1實現接口的時候我們分別在其中加入了一個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。
總結
以上是生活随笔為你收集整理的设计模式之C#实现---Builder的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言单向链表的实现
- 下一篇: 设计模式之C#实现--FactoryMe