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

简介: 摘录自:设计模式与游戏完美开发十年磨一剑,作者将设计模式理论巧妙地融入到实践中,以一个游戏的完整实现呈现设计模式的应用及经验的传承 《轩辕剑》之父——蔡明宏、资深游戏制作人——李佳泽、Product Evangelist at Unity T...

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

十年磨一剑,作者将设计模式理论巧妙地融入到实践中,以一个游戏的完整实现呈现设计模式的应用及经验的传承 《轩辕剑》之父——蔡明宏、资深游戏制作人——李佳泽、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<string,Flyweight> m_Flyweights = new Dictionary<string,Flyweight>();

        // 取得共用的元件 
        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();

    }
}

相关文章
|
19天前
|
设计模式 存储 Java
23种设计模式,享元模式的概念优缺点以及JAVA代码举例
【4月更文挑战第6天】享元模式(Flyweight Pattern)是一种结构型设计模式,旨在通过共享技术有效地支持大量细粒度对象的重用。这个模式在处理大量对象时非常有用,特别是当这些对象中的许多实例实际上可以共享相同的状态时,从而可以减少内存占用,提高程序效率
35 4
|
6月前
|
设计模式 存储 缓存
结构型设计模式07-享元模式
结构型设计模式07-享元模式
20 0
|
4月前
|
设计模式 存储 安全
二十三种设计模式全面解析-享元模式(Flyweight Pattern)详解:构建高效共享的对象结构
二十三种设计模式全面解析-享元模式(Flyweight Pattern)详解:构建高效共享的对象结构
|
4月前
|
设计模式
二十三种设计模式全面解析-组合模式与享元模式的结合应用:实现对象的共享和高效管理
二十三种设计模式全面解析-组合模式与享元模式的结合应用:实现对象的共享和高效管理
|
6月前
|
设计模式 存储 Java
【设计模式——学习笔记】23种设计模式——享元模式Flyweight(原理讲解+应用场景介绍+案例介绍+Java代码实现)
【设计模式——学习笔记】23种设计模式——享元模式Flyweight(原理讲解+应用场景介绍+案例介绍+Java代码实现)
29 0
|
10天前
|
设计模式 存储 Java
小谈设计模式(27)—享元模式
小谈设计模式(27)—享元模式
|
1月前
|
设计模式 缓存 Java
设计模式之享元模式
设计模式之享元模式
|
1月前
|
设计模式 存储 缓存
【设计模式】享元模式
【设计模式】享元模式
|
3月前
|
设计模式 存储 缓存
聊聊Java设计模式-享元模式
享元(Flyweight)模式:顾名思义就是**被共享的单元**。意图是复用对象,节省内存,提升系统的访问效率。比如在红白机冒险岛游戏中的背景花、草、树木等对象,实际上是可以多次被不同场景所复用共享,也是为什么以前的游戏占用那么小的内存,却让我们感觉地图很大的原因。
17 3
聊聊Java设计模式-享元模式
|
8月前
|
设计模式 存储 缓存
享元模式【Java设计模式】
享元模式【Java设计模式】
31 0
享元模式【Java设计模式】