場(chǎng)景:象棋中每粒子都是紅方兩顆黑方兩顆。比如:車,棋盤中總共有4個(gè),常規(guī)做法是有4個(gè)對(duì)象,通過享元1個(gè)對(duì)象搞定。
代碼如下:
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供赤峰林西網(wǎng)站建設(shè)、赤峰林西做網(wǎng)站、赤峰林西網(wǎng)站設(shè)計(jì)、赤峰林西網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、赤峰林西企業(yè)網(wǎng)站模板建站服務(wù),10多年赤峰林西做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
//棋子的外部狀態(tài)
class Protertys
{
public string name { get; set; }
public string position { get; set; }
public string color { get; set; }
public string capacity { get; set; }
public Protertys(string _name,string _position, string _color, string _capacity)
{
name = _name;
position = _position;
color = _color;
capacity = _capacity;
}
}
//棋子類
abstract class Chess
{
protected string name;//內(nèi)部對(duì)象
public Chess(string _name)
{
name = _name;
}
public abstract void Run(Protertys protertys);
}
class ChineseChess : Chess
{
public ChineseChess(string _name) : base(_name)
{
}
public override void Run(Protertys protertys)
{
Console.WriteLine("名字:{0},位置:{1},顏色:{2},性能:{3}", name,protertys.position,protertys.color, protertys.capacity);
}
}
//享元工廠,核心
class ChessFactory
{
private Hashtable hashTable = new Hashtable();
public Chess GetChess(string key)
{
if (!hashTable.ContainsKey(key))
{
hashTable.Add(key,new ChineseChess(key));//不存在就創(chuàng)建
}
return (Chess)hashTable[key];
}
public int GetChessCount()
{
return hashTable.Count;
}
}
//前端
static void Main(string[] args)
{
Protertys protertys = new Protertys("車","左邊第一個(gè)","紅色","走直線");
Protertys protertys1 = new Protertys("車", "右邊第一個(gè)", "紅色", "走直線");
Protertys protertys2 = new Protertys("車", "左邊第一個(gè)", "黑色", "走直線");
Protertys protertys3 = new Protertys("車", "右邊第一個(gè)", "黑色", "走直線");
Protertys protertys4 = new Protertys("馬", "左邊第二個(gè)", "紅色", "走日字");
Protertys protertys5 = new Protertys("馬", "右邊第二個(gè)", "紅色", "走日字");
Protertys protertys6 = new Protertys("馬", "左邊第二個(gè)", "黑色", "走日字");
Protertys protertys7 = new Protertys("馬", "右邊第二個(gè)", "黑色", "走日字");
ChessFactory chessFactory = new ChessFactory();
Chess Chess1= chessFactory.GetChess(protertys.name);
Chess Chess2 = chessFactory.GetChess(protertys1.name);
Chess Chess3 = chessFactory.GetChess(protertys2.name);
Chess Chess4 = chessFactory.GetChess(protertys3.name);
Chess Chess5 = chessFactory.GetChess(protertys4.name);
Chess Chess6 = chessFactory.GetChess(protertys5.name);
Chess Chess7 = chessFactory.GetChess(protertys6.name);
Chess Chess8 = chessFactory.GetChess(protertys7.name);
Chess1.Run(protertys);
Chess2.Run(protertys1);
Chess3.Run(protertys2);
Chess4.Run(protertys3);
Chess5.Run(protertys4);
Chess6.Run(protertys5);
Chess7.Run(protertys6);
Chess8.Run(protertys7);
int count = chessFactory.GetChessCount();
Console.WriteLine("總共有{0}個(gè)對(duì)象",count);
Console.ReadLine();
}
總結(jié):棋盤上的車馬總共是8個(gè)對(duì)象,然后最終只生成了2個(gè)對(duì)象,大大節(jié)約了內(nèi)存。
享元模式就是運(yùn)用共享技術(shù),有效的支持大量細(xì)粒度對(duì)象(字面意思很貼切:共享元數(shù)據(jù))。
方式:把對(duì)象的特性抽離出來當(dāng)外部狀態(tài)然后傳入對(duì)象。
優(yōu)點(diǎn):避免大量的相似的類開銷,可減少對(duì)象的實(shí)例數(shù)量。
缺點(diǎn):程序復(fù)雜化。
和簡(jiǎn)單工廠類似,和單例模式類似。
單例只有一個(gè)實(shí)例,而享元可以有多個(gè)實(shí)例。