设计模式 之 创建者
先把代碼帖上來吧.
這個模式,是模擬 西山居的一款游戲,劍俠情緣 來寫的,呵呵,當然,該游戲到底是怎么設計的,我是不知道,只是作為一個例子放到這個地方.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace 建造者
{
??? public interface 地圖
??? {
??????? 路 路
??????? {
??????????? get;
??????????? set;
??????? }
??????? 樹 樹
??????? {
??????????? get;
??????????? set;
??????? }
??????? Color MainColor
??????? {
??????????? get;
??????????? set;
??????? }
??? }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace 建造者
{
??? public class 長白山南麓 : 地圖
??? {
??????? private 路 tempRoad;
??????? public 路 路
??????? {
??????????? get
??????????? {
??????????????? return tempRoad;
??????????? }
??????????? set
??????????? {
??????????????? tempRoad = value;
??????????? }
??????? }
??????? private 樹 tempTree;
??????? public 樹 樹
??????? {
??????????? get
??????????? {
??????????????? return tempTree;
??????????? }
??????????? set
??????????? {
??????????????? tempTree = value;
??????????? }
??????? }
??????? private System.Drawing.Color tempMainColor;
??????? public System.Drawing.Color MainColor
??????? {
??????????? get
??????????? {
??????????????? return tempMainColor;
??????????? }
??????????? set
??????????? {
??????????????? tempMainColor = value;
??????????? }
??????? }
???????
??? }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace 建造者
{
??? public interface 路
??? {
??????? double Width
??????? {
??????????? get;
??????????? set;
??????? }
??????? double Height
??????? {
??????????? get;
??????????? set;
??????? }
??????? System.Drawing.Color Color
??????? {
??????????? get;
??????????? set;
??????? }
??? }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace 建造者
{
??? public class 水泥路 : 路
??? {
??????? #region 路 Members
??????? public double Width
??????? {
??????????? get
??????????? {
??????????????? throw new Exception("The method or operation is not implemented.");
??????????? }
??????????? set
??????????? {
??????????????? throw new Exception("The method or operation is not implemented.");
??????????? }
??????? }
??????? public double Height
??????? {
??????????? get
??????????? {
??????????????? throw new Exception("The method or operation is not implemented.");
??????????? }
??????????? set
??????????? {
??????????????? throw new Exception("The method or operation is not implemented.");
??????????? }
??????? }
??????? public System.Drawing.Color Color
??????? {
??????????? get
??????????? {
??????????????? throw new Exception("The method or operation is not implemented.");
??????????? }
??????????? set
??????????? {
??????????????? throw new Exception("The method or operation is not implemented.");
??????????? }
??????? }
??????? #endregion
??? }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace 建造者
{
??? public interface 樹
??? {
??????? System.Drawing.Color Color
??????? {
??????????? get;
??????????? set;
??????? }
??? }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace 建造者
{
??? public class 梧桐樹 : 樹
??? {
??????? #region 樹 Members
??????? public System.Drawing.Color Color
??????? {
??????????? get
??????????? {
??????????????? throw new Exception("The method or operation is not implemented.");
??????????? }
??????????? set
??????????? {
??????????????? throw new Exception("The method or operation is not implemented.");
??????????? }
??????? }
??????? #endregion
??? }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace 建造者
{
??? public interface 地圖創建
??? {
??????? void 創建路();
??????? void 創建樹();
??????? void 設置顏色();
??????? 地圖 地圖對象();???
??? }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace 建造者
{
??? public class 長白山南麓地圖創建 : 地圖創建
??? {
??????? private 地圖 長白山南麓地圖;
??????? public 長白山南麓地圖創建()
??????? {
??????????? 長白山南麓地圖 = new 長白山南麓();
??????? }
??????? public void 創建路()
??????? {
??????????? 路 長白山南麓的路 = new 水泥路();
??????????? 長白山南麓地圖.路 = 長白山南麓的路;
??????? }
??????? public void 創建樹()
??????? {
??????????? 樹 長白山南麓的樹 = new 梧桐樹();
??????????? 長白山南麓地圖.樹 = 長白山南麓的樹;
??????? }
??????? public void 設置顏色()
??????? {
??????????? 長白山南麓地圖.MainColor = System.Drawing.Color.White;
??????? }
??????? public 地圖 地圖對象()
??????? {
??????????? return 長白山南麓地圖;
??????? }
??? }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace 建造者
{
?
?public class 系統
??? {
??????? 地圖創建 地圖創建;
??????? public 系統(地圖創建 一個地圖創建實例)
??????? {
??????????? 地圖創建 = 一個地圖創建實例;
??????? }
??????? public 地圖 創建地圖()
??????? {
??????????? 地圖創建.創建路();
??????????? 地圖創建.創建樹();
??????????? 地圖創建.設置顏色();
??????????? return 地圖創建.地圖對象();
??????? }
??? }
}
客戶端調用:
?
??????????? 建造者.系統 建造者系統 = new 建造者.系統(new 建造者.長白山南麓地圖創建());
??????????
??????????? 建造者.地圖 長白山南麓地圖 = 建造者系統.創建地圖();
我很苦惱,按照這樣的做法,怎么越來越象 工廠方法了.
呵呵,也不知道這個模式有沒有寫對.
先把代碼放到這個地方,有時間在研究 創建者? 和工廠方法的區別.
轉載于:https://www.cnblogs.com/yanchanggang/archive/2007/12/18/1004225.html
總結
以上是生活随笔為你收集整理的设计模式 之 创建者的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Powerful array CodeF
- 下一篇: 生活规则