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
  • 相关阅读:
    Spring + MySQL + Mybatis + Redis【二级缓存】执行流程分析
    Spring + MySQL + Mybatis + Redis【二级缓存】
    MyBatis的笔记
    Spring事务:一种编程式事务,三种声明式事务
    笔记
    mybatis-generator自定义注释生成
    做准备的笔记
    常用DOS命令和Linux命令
    数据库MongoDB查询语句--持续更新
    SpringBoot集成websocket实现后端向页面发送消息
  • 原文地址:https://www.cnblogs.com/starcrm/p/1358144.html
Copyright © 2011-2022 走看看