设计模式---享元模式(DesignPattern_Flyweight)

摘录自:设计模式与游戏完美开发

十年磨一剑,作者将设计模式理论巧妙地融入到实践中,以一个游戏的完整实现呈现设计模式的应用及经验的传承 《轩辕剑》之父——蔡明宏、资深游戏制作人——李佳泽、Product Evangelist at Unity Technologies——Kelvin Lo、信仁软件设计创办人—— 赖信仁、资深3D游戏美术——刘明恺 联合推荐全书采用了整合式的项目教学,即以一个游戏的范例来应用23种设计模式的实现贯穿全书,让读者学习到整个游戏开发的全过程和作者想要传承的经验,并以浅显易懂的比喻来解析难以理解的设计模式,让想深入了解此领域的读者更加容易上手。


工程GitHub

FLYWEIGHT—每天跟MM发短信,手指都累死了,最近买了个新手机,可以把一些常用的句子存在手机里,要用的时候,直接拿出来,在前面加上MM的名字就可以发送了,再不用一个字一个字敲了。共享的句子就是Flyweight,MM的名字就是提取出来的外部特征,根据上下文情况使用。

享元模式:FLYWEIGHT在拳击比赛中指最轻量级。享元模式以共享的方式高效的支持大量的细粒度对象。享元模式能做到共享的关键是区分内蕴状态和外蕴状态。内蕴状态存储在享元内部,不会随环境的改变而有所不同。外蕴状态是随环境的改变而改变的。外蕴状态不能影响内蕴状态,它们是相互独立的。将可以共享的状态和不可以共享的状态从常规类中区分开来,将不可以共享的状态从类里剔除出去。客户端不可以直接创建被共享的对象,而应当使用一个工厂对象负责创建被共享的对象。享元模式大幅度的降低内存中对象的数量。

using UnityEngine;
using System.Collections.Generic;
namespace DesignPattern_Flyweight
{
    // 可以被共用的Flyweight介面    
    public abstract class Flyweight
    {
        protected string m_Content; //显示的內容
        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;
        }

        // 设定共享的元件
        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);
        }
    }

    // 负责产生Flyweight的工厂介面
    public class FlyweightFactor
    {
        Dictionary m_Flyweights = new Dictionary();

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

            // 产生并且设定內容
            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);

            // 产出元件
            UnsharedConcreteFlyweight  theFlyweight =  new UnsharedConcreteFlyweight( UnsharedContent);
            theFlyweight.SetFlyweight( SharedFlyweight ); // 設定共享的部份
            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();

        // 产生共用元件
        theFactory.GetFlyweight("1","共用元件1");
        theFactory.GetFlyweight("2","共用元件1");
        theFactory.GetFlyweight("3","共用元件1");

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

        // 产生不共用的元件
        UnsharedConcreteFlyweight theUnshared1 = theFactory.GetUnsharedFlyweight("不共用的资讯1");
        theUnshared1.Operator();

        // 设定共用元件
        theUnshared1.SetFlyweight( theFlyweight );

        // 产生不共用的元件2,並指定使用共用元件1
        UnsharedConcreteFlyweight theUnshared2 = theFactory.GetUnsharedFlyweight("1","","不共用的资讯2");

        // 同时显示
        theUnshared1.Operator();
        theUnshared2.Operator();

    }
}

你可能感兴趣的:(设计模式---享元模式(DesignPattern_Flyweight))