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
  • 相关阅读:
    通过 WakaTime 统计你写代码的时长
    CCF 202012-3 带配额的文件系统
    1
    prometheus 获取cpu利用率
    springboot使用@data注解,减少不必要代码-lombok插件
    django官方教程部署simpleui时候发现加载不到静态文件解决办法
    echarts关系图研究01
    SpringBoot代码方式禁用Druid Monitor
    virtualbox给已有磁盘扩展容量
    centos7 ssh免密登录配置
  • 原文地址:https://www.cnblogs.com/starcrm/p/1358144.html
Copyright © 2011-2022 走看看