設(shè)計(jì)模式---享元模式(DesignPattern_Flyweight)

摘錄自:設(shè)計(jì)模式與游戲完美開發(fā)

十年磨一劍,作者將設(shè)計(jì)模式理論巧妙地融入到實(shí)踐中,以一個(gè)游戲的完整實(shí)現(xiàn)呈現(xiàn)設(shè)計(jì)模式的應(yīng)用及經(jīng)驗(yàn)的傳承 《軒轅劍》之父——蔡明宏、資深游戲制作人——李佳澤、Product Evangelist at Unity Technologies——Kelvin Lo、信仁軟件設(shè)計(jì)創(chuàng)辦人—— 賴信仁、資深3D游戲美術(shù)——?jiǎng)⒚鲪? 聯(lián)合推薦全書采用了整合式的項(xiàng)目教學(xué),即以一個(gè)游戲的范例來應(yīng)用23種設(shè)計(jì)模式的實(shí)現(xiàn)貫穿全書,讓讀者學(xué)習(xí)到整個(gè)游戲開發(fā)的全過程和作者想要傳承的經(jīng)驗(yàn),并以淺顯易懂的比喻來解析難以理解的設(shè)計(jì)模式,讓想深入了解此領(lǐng)域的讀者更加容易上手。


工程GitHub

FLYWEIGHT—每天跟MM發(fā)短信,手指都累死了,最近買了個(gè)新手機(jī),可以把一些常用的句子存在手機(jī)里,要用的時(shí)候,直接拿出來,在前面加上MM的名字就可以發(fā)送了,再不用一個(gè)字一個(gè)字敲了。共享的句子就是Flyweight,MM的名字就是提取出來的外部特征,根據(jù)上下文情況使用。

享元模式:FLYWEIGHT在拳擊比賽中指最輕量級(jí)。享元模式以共享的方式高效的支持大量的細(xì)粒度對(duì)象。享元模式能做到共享的關(guān)鍵是區(qū)分內(nèi)蘊(yùn)狀態(tài)和外蘊(yùn)狀態(tài)。內(nèi)蘊(yùn)狀態(tài)存儲(chǔ)在享元內(nèi)部,不會(huì)隨環(huán)境的改變而有所不同。外蘊(yùn)狀態(tài)是隨環(huán)境的改變而改變的。外蘊(yùn)狀態(tài)不能影響內(nèi)蘊(yùn)狀態(tài),它們是相互獨(dú)立的。將可以共享的狀態(tài)和不可以共享的狀態(tài)從常規(guī)類中區(qū)分開來,將不可以共享的狀態(tài)從類里剔除出去。客戶端不可以直接創(chuàng)建被共享的對(duì)象,而應(yīng)當(dāng)使用一個(gè)工廠對(duì)象負(fù)責(zé)創(chuàng)建被共享的對(duì)象。享元模式大幅度的降低內(nèi)存中對(duì)象的數(shù)量。

using UnityEngine;
using System.Collections.Generic;
namespace DesignPattern_Flyweight
{
    // 可以被共用的Flyweight介面    
    public abstract class Flyweight
    {
        protected string m_Content; //顯示的內(nèi)容
        public Flyweight(){}
        public Flyweight(string Content)
        {
            m_Content= Content;
        }
        public string GetContent()
        {
            return m_Content;
        }
        public abstract void Operator();

    }

    // 共用的元件
    public class ConcreteFlyweight : Flyweight
    {
        public ConcreteFlyweight(string Content):base( Content )
        {
        }

        public override void Operator()
        {
            Debug.Log("ConcreteFlyweight.Content["+m_Content+"]");
        }
    }

    // 不共用的元件(可以不必繼承)
    public class UnsharedConcreteFlyweight  //: Flyweight
    {
        Flyweight m_Flyweight = null; // 共享的元件
        string m_UnsharedContent;     // 不共享的元件

        public UnsharedConcreteFlyweight(string Content)
        {
            m_UnsharedContent = Content;
        }

        // 設(shè)定共享的元件
        public void SetFlyweight(Flyweight theFlyweight)
        {
            m_Flyweight = theFlyweight;
        }
        
        public void Operator()
        {
            string Msg = string.Format("UnsharedCoincreteFlyweight.Content[{0}]",m_UnsharedContent);
            if( m_Flyweight != null)
                Msg += "包含了:" + m_Flyweight.GetContent();
            Debug.Log(Msg);
        }
    }

    // 負(fù)責(zé)產(chǎn)生Flyweight的工廠介面
    public class FlyweightFactor
    {
        Dictionary<string,Flyweight> m_Flyweights = new Dictionary<string,Flyweight>();

        // 取得共用的元件 
        public Flyweight GetFlyweight(string Key,string Content)
        {
            if( m_Flyweights.ContainsKey( Key) )
                return m_Flyweights[Key];

            // 產(chǎn)生并且設(shè)定內(nèi)容
            ConcreteFlyweight theFlyweight = new ConcreteFlyweight( Content );
            m_Flyweights[Key] = theFlyweight;
            Debug.Log ("New ConcreteFlyweigh Key["+Key+"] Content["+Content+"]");
            return theFlyweight;
        }

        // 取得元件(只取得不共用的Flyweight)
        public UnsharedConcreteFlyweight GetUnsharedFlyweight(string Content)
        {
            return new UnsharedConcreteFlyweight( Content);
        }

        // 取得元件(包含共用部份的Flyweight)
        public UnsharedConcreteFlyweight GetUnsharedFlyweight(string Key,string SharedContent,string UnsharedContent)
        {
            // 先取得共用的部份
            Flyweight SharedFlyweight = GetFlyweight(Key, SharedContent);

            // 產(chǎn)出元件
            UnsharedConcreteFlyweight  theFlyweight =  new UnsharedConcreteFlyweight( UnsharedContent);
            theFlyweight.SetFlyweight( SharedFlyweight ); // 設(shè)定共享的部份
            return theFlyweight;
        }
    }
}
using UnityEngine;
using System.Collections;
using DesignPattern_Flyweight;

public class FlyweightTest : MonoBehaviour {

    // Use this for initialization
    void Start () {
        UnitTest(); 
    }

    void UnitTest () {

        // 元件工廠
        FlyweightFactor theFactory = new FlyweightFactor();

        // 產(chǎn)生共用元件
        theFactory.GetFlyweight("1","共用元件1");
        theFactory.GetFlyweight("2","共用元件1");
        theFactory.GetFlyweight("3","共用元件1");

        // 取得一個(gè)共用元件
        Flyweight theFlyweight = theFactory.GetFlyweight("1","");
        theFlyweight.Operator();

        // 產(chǎn)生不共用的元件
        UnsharedConcreteFlyweight theUnshared1 = theFactory.GetUnsharedFlyweight("不共用的資訊1");
        theUnshared1.Operator();

        // 設(shè)定共用元件
        theUnshared1.SetFlyweight( theFlyweight );

        // 產(chǎn)生不共用的元件2,並指定使用共用元件1
        UnsharedConcreteFlyweight theUnshared2 = theFactory.GetUnsharedFlyweight("1","","不共用的資訊2");

        // 同時(shí)顯示
        theUnshared1.Operator();
        theUnshared2.Operator();

    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容