您好,登錄后才能下訂單哦!
場(chǎng)景:象棋中每粒子都是紅方兩顆黑方兩顆。比如:車,棋盤中總共有4個(gè),常規(guī)做法是有4個(gè)對(duì)象,通過(guò)享元1個(gè)對(duì)象搞定。
代碼如下:
//棋子的外部狀態(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ì)象的特性抽離出來(lái)當(dāng)外部狀態(tài)然后傳入對(duì)象。
優(yōu)點(diǎn):避免大量的相似的類開銷,可減少對(duì)象的實(shí)例數(shù)量。
缺點(diǎn):程序復(fù)雜化。
和簡(jiǎn)單工廠類似,和單例模式類似。
單例只有一個(gè)實(shí)例,而享元可以有多個(gè)實(shí)例。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。