分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

Definition

Use sharing to support large numbers of fine-grained objects efficiently.

Participants

The classes and/or objects participating in this pattern are:

  • Flyweight (Character)

    • Declares an interface through which flyweights can receive and act on extrinsic state.
  • ConcreteFlyweight (CharacterA, CharacterB, ..., CharacterZ)
    • Implements the Flyweight interface and adds storage for intrinsic state, if any. A ConcreteFlyweight object must be sharable. Any state it stores must be intrinsic, that is, it must be independent of the ConcreteFlyweight object's context.
  • UnsharedConcreteFlyweight (not used)
    • Not all Flyweight subclasses need to be shared. The Flyweight interface enables sharing, but it doesn't enforce it. It is common for UnsharedConcreteFlyweight objects to have ConcreteFlyweight objects as children at some level in the flyweight object structure (as the Row and Column classes have).
  • FlyweightFactory (CharacterFactory)
    • Creates and manages flyweight objects
    • Ensures that flyweight are shared properly. When a client requests a flyweight, the FlyweightFactory objects assets an existing instance or creates one, if none exists.
  • Client (FlyweightApp)
    • Maintains a reference to flyweight(s).
    • Computes or stores the extrinsic state of flyweight(s).

Sample code in C#


This structural code demonstrates the Flyweight pattern in which a relatively small number of objects is shared many times by different clients.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Structural Flyweight Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    using System.Collections;    /// <summary>    /// Startup class for Structural Flyweight Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            // Arbitrary extrinsic state            int extrinsicstate = 22;            var factory = new FlyweightFactory();            // Work with different flyweight instances            Flyweight fx = factory.GetFlyweight("X");            fx.Operation(--extrinsicstate);            Flyweight fy = factory.GetFlyweight("Y");            fy.Operation(--extrinsicstate);            Flyweight fz = factory.GetFlyweight("Z");            fz.Operation(--extrinsicstate);            var fu = new UnsharedConcreteFlyweight();            fu.Operation(--extrinsicstate);        }        #endregion    }    /// <summary>    /// The 'FlyweightFactory' class    /// </summary>    internal class FlyweightFactory    {        #region Fields        /// <summary>        /// The flyweights.        /// </summary>        private Hashtable flyweights = new Hashtable();        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="FlyweightFactory"/> class.        /// </summary>        public FlyweightFactory()        {            this.flyweights.Add("X", new ConcreteFlyweight());            this.flyweights.Add("Y", new ConcreteFlyweight());            this.flyweights.Add("Z", new ConcreteFlyweight());        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The get flyweight.        /// </summary>        /// <param name="key">        /// The key.        /// </param>        /// <returns>        /// The <see cref="Flyweight"/>.        /// </returns>        public Flyweight GetFlyweight(string key)        {            return (Flyweight)this.flyweights[key];        }        #endregion    }    /// <summary>    /// The 'Flyweight' abstract class    /// </summary>    internal abstract class Flyweight    {        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        /// <param name="extrinsicstate">        /// The extrinsic state.        /// </param>        public abstract void Operation(int extrinsicstate);        #endregion    }    /// <summary>    /// The 'ConcreteFlyweight' class    /// </summary>    internal class ConcreteFlyweight : Flyweight    {        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        /// <param name="extrinsicstate">        /// The extrinsic state.        /// </param>        public override void Operation(int extrinsicstate)        {            Console.WriteLine("ConcreteFlyweight: " + extrinsicstate);        }        #endregion    }    /// <summary>    /// The 'UnsharedConcreteFlyweight' class    /// </summary>    internal class UnsharedConcreteFlyweight : Flyweight    {        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        /// <param name="extrinsicstate">        /// The extrinsic state.        /// </param>        public override void Operation(int extrinsicstate)        {            Console.WriteLine("UnsharedConcreteFlyweight: " + extrinsicstate);        }        #endregion    }}// Output:/*ConcreteFlyweight: 21ConcreteFlyweight: 20ConcreteFlyweight: 19UnsharedConcreteFlyweight: 18*/

This real-world code demonstrates the Flyweight pattern in which a relatively small number of Character objects is shared many times by a document that has potentially many characters.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Flyweight Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    using System.Collections.Generic;    /// <summary>    /// Startup class for Real-World Flyweight Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            // Build a document with text            const string Document = "AAZZBBZB";            char[] chars = Document.ToCharArray();            var factory = new CharacterFactory();            // extrinsic state            int pointSize = 10;            // For each character use a flyweight object            foreach (char c in chars)            {                pointSize++;                Character character = factory.GetCharacter(c);                character.Display(pointSize);            }        }        #endregion    }    /// <summary>    /// The 'FlyweightFactory' class    /// </summary>    internal class CharacterFactory    {        #region Fields        /// <summary>        /// The characters.        /// </summary>        private Dictionary<char, Character> characters = new Dictionary<char, Character>();        #endregion        #region Public Methods and Operators        /// <summary>        /// The get character.        /// </summary>        /// <param name="key">        /// The key.        /// </param>        /// <returns>        /// The <see cref="Character"/>.        /// </returns>        public Character GetCharacter(char key)        {            // Uses "lazy initialization"            Character character = null;            if (this.characters.ContainsKey(key))            {                character = this.characters[key];            }            else            {                switch (key)                {                    case 'A':                        character = new CharacterA();                        break;                    case 'B':                        character = new CharacterB();                        break;                    // ...                    case 'Z':                        character = new CharacterZ();                        break;                }                this.characters.Add(key, character);            }            return character;        }        #endregion    }    /// <summary>    /// The 'Flyweight' abstract class    /// </summary>    internal abstract class Character    {        #region Fields        /// <summary>        /// The ascent.        /// </summary>        protected int Ascent;        /// <summary>        /// The descent.        /// </summary>        protected int Descent;        /// <summary>        /// The height.        /// </summary>        protected int Height;        /// <summary>        /// The point size.        /// </summary>        protected int PointSize;        /// <summary>        /// The symbol.        /// </summary>        protected char Symbol;        /// <summary>        /// The width.        /// </summary>        protected int Width;        #endregion        #region Public Methods and Operators        /// <summary>        /// The display.        /// </summary>        /// <param name="pointSize">        /// The point size.        /// </param>        public abstract void Display(int pointSize);        #endregion    }    /// <summary>    /// A 'ConcreteFlyweight' class    /// </summary>    internal class CharacterA : Character    {        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="CharacterA"/> class.        /// </summary>        public CharacterA()        {            this.Symbol = 'A';            this.Height = 100;            this.Width = 120;            this.Ascent = 70;            this.Descent = 0;        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The display.        /// </summary>        /// <param name="pointSize">        /// The point size.        /// </param>        public override void Display(int pointSize)        {            this.PointSize = pointSize;            Console.WriteLine(this.Symbol + " (point size " + this.PointSize + ")");        }        #endregion    }    /// <summary>    /// A 'ConcreteFlyweight' class    /// </summary>    internal class CharacterB : Character    {        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="CharacterB"/> class.        /// </summary>        public CharacterB()        {            this.Symbol = 'B';            this.Height = 100;            this.Width = 140;            this.Ascent = 72;            this.Descent = 0;        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The display.        /// </summary>        /// <param name="pointSize">        /// The point size.        /// </param>        public override void Display(int pointSize)        {            this.PointSize = pointSize;            Console.WriteLine(this.Symbol + " (point size " + this.PointSize + ")");        }        #endregion    }    // ... C, D, E, etc.    /// <summary>    /// A 'ConcreteFlyweight' class    /// </summary>    internal class CharacterZ : Character    {        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="CharacterZ"/> class.        /// </summary>        public CharacterZ()        {            this.Symbol = 'Z';            this.Height = 100;            this.Width = 100;            this.Ascent = 68;            this.Descent = 0;        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The display.        /// </summary>        /// <param name="pointSize">        /// The point size.        /// </param>        public override void Display(int pointSize)        {            this.PointSize = pointSize;            Console.WriteLine(this.Symbol + " (point size " + this.PointSize + ")");        }        #endregion    }}// Output:/*A (point size 11)A (point size 12)Z (point size 13)Z (point size 14)B (point size 15)B (point size 16)Z (point size 17)B (point size 18)*/

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

Design Pattern - Flyweight(C#)相关推荐

  1. Design Pattern - Visitor(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  2. Design Pattern - State(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  3. Design Pattern - Observer(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  4. Design Pattern - Mediator(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  5. Design Pattern - Memento(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  6. Design Pattern - Iterator(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  7. Design Pattern - Interpreter(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  8. Design Pattern - Command (C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  9. Design Pattern - Proxy(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

最新文章

  1. 2019百度之星初赛-1
  2. 剖析boot.img的制作流程
  3. 如何给ABAP类自动生成帮助文档
  4. 前端学习(1407):多人管理27代码优化
  5. mikechen详谈架构师成长之3大步骤
  6. python modbus tk 库_python modbus_tk模块学习笔记(rtu slaver例程)
  7. 【数据结构】BFS 代码模板
  8. python装饰器用法
  9. HFSS15.0新手村任务
  10. 【ArcGIS教程】ArcGIS软件操作——地图配准
  11. 8.0强行转换后变成了7_南方Cass软件坐标转换方法!
  12. 2018第三方支付牌照公司
  13. 在CENTOS 7上安装SNIPE-IT进行资产管理
  14. 三星s4流量显示无服务器,三星s4有什么隐藏功能
  15. 服务器提取用户信息,获取客户端和服务器信息
  16. KUDU(三)kudu的模式设计
  17. Linux下文件备份和同步的工具软件
  18. FIR内插滤波器结构与代码实现
  19. innodb_flush_method 的理解
  20. iqooneo3 如何不用vivo账号下载外部应用_iQOO Neo3评测:救市或转型?总之香就对了...

热门文章

  1. 2010年7月微软最有价值专家(MVP)当选名单
  2. PHP带头大哥谈程序语言的学习体会!
  3. 爱德华·斯诺登:区块链只是新型数据库,比特币终会消失
  4. 使用Innobackupex快速搭建(修复)MySQL主从架构
  5. 最佳时间 (DOM编程艺术)
  6. Entity Framework返回IEnumerable还是IQueryable?
  7. column 'XXXX' in field list is ambiguous
  8. 获取点击的键盘的keyCode
  9. linux centos7 升级gcc版本 使用 yum centos-release-scl devtoolset-8-gcc* 非源码编译
  10. golang 获取公网ip 内网ip 检测ip类型 校验ip区间 ip地址string和int转换 判断ip地区国家运营商