设计模式之C#实现---Builder
我們將要介紹一個(gè)和它比較像的創(chuàng)建型模式 Builder (至于關(guān)于 Builder 的詳細(xì)內(nèi)容您可以參考 GOF 的書,在這里不重復(fù)了。)。在 GOF 的書里 Builder 的目的是這樣的: Separate the construction of a complex object from its representation so that the same construction process can create different representations. 在我的程序設(shè)計(jì)中很難免會(huì)使用復(fù)雜的對(duì)象比如:車的組成、電腦的組成以及人在內(nèi)。那么我們?cè)趧?chuàng)建電腦這個(gè)對(duì)象的時(shí)候我想我們需要一步一步的創(chuàng)建組成電腦的每一部分,先創(chuàng)建 CPU 對(duì)象、 Memory 對(duì)象、 HardDisk 對(duì)象等等。 Builder 就是這樣一種模式用來一步一步的創(chuàng)建對(duì)象的每一部分。回憶一下 AbstractFactory 也是創(chuàng)建一族相關(guān)的對(duì)象, Builder 也是創(chuàng)建一些相關(guān)的對(duì)象,兩者之間的區(qū)別很微妙需要在以后的實(shí)踐中細(xì)細(xì)體會(huì)。
既然文章叫設(shè)計(jì)模式之C#實(shí)現(xiàn)那么肯定少不了代碼了,這次我想說的更清楚一些,我打算從如下兩個(gè)方面來實(shí)現(xiàn),首先我想要直接實(shí)現(xiàn)他的結(jié)構(gòu),也就是我們?cè)谙旅娴膱D中看到的那些類。接著我將用以個(gè)具體的例子或者書上的例子描述一下用來加深理解,希望我的描述可以幫助你更好的學(xué)習(xí)。
?????? 從圖上我們可以看出我們的Builder接口中有兩個(gè)BuilderPart方法A、B,以及一個(gè)GetResult方法用來返回創(chuàng)建的對(duì)象。將我們用ConcreteBuilder1和ConcreteBuilder1實(shí)現(xiàn)接口的時(shí)候我們分別在其中加入了一個(gè)Private的對(duì)象,用來返回建立好的對(duì)象,在該實(shí)例的內(nèi)部則是經(jīng)過了兩步才完成了Product對(duì)象的初始化。我們建立的Product是由一個(gè)Hashtable組成,可以添加和顯示自己的每一個(gè)部分(就是Hashtable里面的每一個(gè)鍵/值)。好了不廢話了看看下面的實(shí)現(xiàn)代碼,在WinForm中調(diào)試通過,你可以參看本系列的AbstractFactory文章找到里面的相關(guān)表現(xiàn)對(duì)象(RichTextBox)。
?????? 代碼中有少量的注釋是為了更好的理解。
using System;
namespace Builder_Me{
?????? using System.Collections;
?????? // specifies an abstract interface for creating parts of a Product object.
?????? //為創(chuàng)建對(duì)象的一個(gè)部分指定一個(gè)接口
?????? 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(){
???????????????????? //必須先調(diào)用該方法否則不能實(shí)例化對(duì)象
???????????????????? 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.
?????? //要?jiǎng)?chuàng)建復(fù)雜的對(duì)象該對(duì)象我們用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++實(shí)現(xiàn)所以轉(zhuǎn)換成C#也非常容易,我在這里就不轉(zhuǎn)換了,有興趣的人可以轉(zhuǎn)換跟帖。
本人能力有限,如果在上面有什么說錯(cuò)的或者不準(zhǔn)確地方請(qǐng)網(wǎng)友指正,我將虛心學(xué)習(xí),我的email:wu_jian830@hotmail.com。
總結(jié)
以上是生活随笔為你收集整理的设计模式之C#实现---Builder的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言单向链表的实现
- 下一篇: 设计模式之C#实现--FactoryMe