設(shè)計模式-享元模式-創(chuàng)新互聯(lián)

場景:象棋中每粒子都是紅方兩顆黑方兩顆。比如:車,棋盤中總共有4個,常規(guī)做法是有4個對象,通過享元1個對象搞定。
代碼如下:

成都創(chuàng)新互聯(lián)公司是網(wǎng)站建設(shè)專家,致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,專業(yè)領(lǐng)域包括網(wǎng)站設(shè)計制作、網(wǎng)站制作、電商網(wǎng)站制作開發(fā)、微信小程序定制開發(fā)、微信營銷、系統(tǒng)平臺開發(fā),與其他網(wǎng)站設(shè)計及系統(tǒng)開發(fā)公司不同,我們的整合解決方案結(jié)合了恒基網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,且不斷評估并優(yōu)化我們的方案,為客戶提供全方位的互聯(lián)網(wǎng)品牌整合方案!
//棋子的外部狀態(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)部對象
        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("車","左邊第一個","紅色","走直線");
            Protertys protertys1 = new Protertys("車", "右邊第一個", "紅色", "走直線");
            Protertys protertys2 = new Protertys("車", "左邊第一個", "黑色", "走直線");
            Protertys protertys3 = new Protertys("車", "右邊第一個", "黑色", "走直線");

            Protertys protertys4 = new Protertys("馬", "左邊第二個", "紅色", "走日字");
            Protertys protertys5 = new Protertys("馬", "右邊第二個", "紅色", "走日字");
            Protertys protertys6 = new Protertys("馬", "左邊第二個", "黑色", "走日字");
            Protertys protertys7 = new Protertys("馬", "右邊第二個", "黑色", "走日字");

            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}個對象",count);
            Console.ReadLine();
        }

總結(jié):棋盤上的車馬總共是8個對象,然后最終只生成了2個對象,大大節(jié)約了內(nèi)存。
享元模式就是運(yùn)用共享技術(shù),有效的支持大量細(xì)粒度對象(字面意思很貼切:共享元數(shù)據(jù))。
方式:把對象的特性抽離出來當(dāng)外部狀態(tài)然后傳入對象。
優(yōu)點(diǎn):避免大量的相似的類開銷,可減少對象的實(shí)例數(shù)量。
缺點(diǎn):程序復(fù)雜化。

和簡單工廠類似,和單例模式類似。
單例只有一個實(shí)例,而享元可以有多個實(shí)例。
設(shè)計模式-享元模式

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。

當(dāng)前名稱:設(shè)計模式-享元模式-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://muchs.cn/article20/djgoco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、App開發(fā)、網(wǎng)站維護(hù)響應(yīng)式網(wǎng)站、定制開發(fā)、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司