当前位置: 首页 > news >正文

设计模式---享元模式(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<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();

    }
}

相关文章:

  • 计算机宏
  • ES内部分片处理机制——Segment
  • 30 天精通 RxJS(18): Observable Operators - switchMap, mergeMap, concatMap
  • python数据结构之 set
  • Gartner全球IAAS市场报告:阿里云进入全球前三
  • 问题-MethodAddress返回NIL?MethodAddress与published的关系?
  • 【批处理学习笔记】第二十五课:间接传递
  • 献身说法---修复bug时的一些小技巧
  • 老调重弹-access注入过主机卫
  • awk 截取某段时间的日志
  • 表单中readonly的input等标签,禁止光标进入(focus)的几种方式
  • vue从创建到完整的饿了么(18)购物车详细信息的展示与删除
  • windows下VisualStudio和QtCreator搭建Qt开发环境
  • mysql 基础学习1
  • Spring第一个helloWorld
  • [分享]iOS开发-关于在xcode中引用文件夹右边出现问号的解决办法
  • 【node学习】协程
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • Docker下部署自己的LNMP工作环境
  • HTML5新特性总结
  • IndexedDB
  • leetcode46 Permutation 排列组合
  • Meteor的表单提交:Form
  • Netty源码解析1-Buffer
  • Python学习之路16-使用API
  • SpiderData 2019年2月25日 DApp数据排行榜
  • webpack入门学习手记(二)
  • Work@Alibaba 阿里巴巴的企业应用构建之路
  • 百度贴吧爬虫node+vue baidu_tieba_crawler
  • 创建一种深思熟虑的文化
  • 搭建gitbook 和 访问权限认证
  • 微服务框架lagom
  • 问题之ssh中Host key verification failed的解决
  • 在Mac OS X上安装 Ruby运行环境
  • 2017年360最后一道编程题
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • ​​​​​​​sokit v1.3抓手机应用socket数据包: Socket是传输控制层协议,WebSocket是应用层协议。
  • #{}和${}的区别?
  • (ZT)薛涌:谈贫说富
  • (黑客游戏)HackTheGame1.21 过关攻略
  • (论文阅读31/100)Stacked hourglass networks for human pose estimation
  • (转)http-server应用
  • (转)JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
  • (转)ObjectiveC 深浅拷贝学习
  • (轉貼) VS2005 快捷键 (初級) (.NET) (Visual Studio)
  • .net 8 发布了,试下微软最近强推的MAUI
  • .net core 3.0 linux,.NET Core 3.0 的新增功能
  • .net core控制台应用程序初识
  • .Net Framework 4.x 程序到底运行在哪个 CLR 版本之上
  • .net 逐行读取大文本文件_如何使用 Java 灵活读取 Excel 内容 ?
  • .NET简谈互操作(五:基础知识之Dynamic平台调用)
  • .net快速开发框架源码分享
  • .NET中的Event与Delegates,从Publisher到Subscriber的衔接!
  • [ C++ ] STL priority_queue(优先级队列)使用及其底层模拟实现,容器适配器,deque(双端队列)原理了解
  • []T 还是 []*T, 这是一个问题