zoukankan      html  css  js  c++  java
  • 一个没有方法继承的Flyweight模式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace Flyweight
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string document = "AAZZBBZBAAZZBBZBAAZZBBZB";

                char[] chars = document.ToCharArray();
                int charsCount=0;

         

                  CharacterFactory factory = new CharacterFactory();

                  foreach (char c in chars)

                  {

                    charsCount++;

                    Character character = factory.GetCharacter(c);

                     Response.Write(character.Display(charsCount,"#0000FF"));

                  }
                Response.Write("共"+charsCount+"字符");

            
               }


            }


        }

        class CharacterFactory
        {

            private Dictionary<char, Character> _characters =

              new Dictionary<char, Character>();

            public Character GetCharacter(char key)
            {

                // Uses "lazy initialization"

                Character character = null;

                if (_characters.ContainsKey(key))
                {

                    character = _characters[key];

                }

                else
                {

                    switch (key)
                    {

                        case 'A': character = new CharacterA(); break;

                        case 'B': character = new CharacterB(); break;

                        //...

                        case 'Z': character = new CharacterZ(); break;

                    }

                    _characters.Add(key, character);

                }

                return character;

            }

        }

        /// <summary>

        /// The 'Flyweight' abstract class

        /// </summary>

        abstract class Character
        {

            protected char symbol;

            protected int width;

            protected int height;

            protected string color;

            protected int descent;

            protected int fontsize;

            public string Display(int fontsize, string color)
            {

                this.fontsize = fontsize;
                this.color = color;

                return "<FONT SIZE=" + fontsize + " COLOR=" + color + ">" + this.symbol + "</FONT>";
            }
        }

        /// <summary>

        /// A 'ConcreteFlyweight' class

        /// </summary>

        class CharacterA : Character
        {

            // Constructor

            public CharacterA()
            {

                this.symbol = 'A';

            }

            //public override string Display(int fontsize, string color)
            //{

            //    this.fontsize = fontsize;
            //    this.color = color;

            //    return "<FONT SIZE=" + fontsize + " COLOR=" + color + ">" + this.symbol + "</FONT>";
            //}

        }

        /// <summary>

        /// A 'ConcreteFlyweight' class

        /// </summary>

        class CharacterB : Character
        {

            // Constructor

            public CharacterB()
            {

                this.symbol = 'B';

            }


        }


       class CharacterZ : Character
        {

            // Constructor

            public CharacterZ()
            {

                this.symbol = 'Z';

            }

        }


     

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    和菜鸟一起学linux之bluetooth学习记录基础知识
    教父马云的经典语录汇总
    win32 多线程基础
    自己收集整理的微软错误代码大全(中文和英文)
    如何解决数据库中的字符型字段值中包含'0A'时,导出的文件用EXECEL打开时行数变多或者将结果导入数据库出错
    win32 TCP网络文件传输服务器端1.23
    Android窗口管理服务WindowManagerService对窗口的组织方式分析
    和菜鸟一起学linux总线驱动之DMA传输
    win 32下c语言文件传输客户端1.23
    Resolution to the record count increasing of the file exported from DB when ‘0A’ is included in it
  • 原文地址:https://www.cnblogs.com/starcrm/p/1358144.html
Copyright © 2011-2022 走看看