日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發布時間:2023/11/27 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式之C#实现---Builder 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作者: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 就是這樣一種模式用來一步一步的創建對象的每一部分?;貞浺幌?/span> AbstractFactory 也是創建一族相關的對象, Builder 也是創建一些相關的對象,兩者之間的區別很微妙需要在以后的實踐中細細體會。

既然文章叫設計模式之C#實現那么肯定少不了代碼了,這次我想說的更清楚一些,我打算從如下兩個方面來實現,首先我想要直接實現他的結構,也就是我們在下面的圖中看到的那些類。接著我將用以個具體的例子或者書上的例子描述一下用來加深理解,希望我的描述可以幫助你更好的學習。

?????? 從圖上我們可以看出我們的Builder接口中有兩個BuilderPart方法A、B,以及一個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

總結

以上是生活随笔為你收集整理的设计模式之C#实现---Builder的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。