现在的位置: 首页 > 综合 > 正文

一个没有方法继承的Flyweight模式

2012年12月28日 ⁄ 综合 ⁄ 共 2681字 ⁄ 字号 评论关闭

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';

        }

 

    }

 

 

 

抱歉!评论已关闭.