본문 바로가기
C# 디자인패턴

13# Flyweight패턴 Structural Patterns

by NaHyungMin 2019. 6. 19.

 

static void Main(string[] args)

{

    //BuilderExample();

    //FactoryExample();

    //FactoryMethodExample();

    //FactoryMethodExample2();

    //ProtoTypeExample();

    //SingletonExample();

    //AdapterExample();

    //BridgeExample();

    //CompositeExample();

    //DecoratorExample();

    //FacadeExample();

 

    FlyweightExample();

 

    //Console.WriteLine("Hello World!");

    Console.ReadKey();

}

 

static void FlyweightExample()

{

    string document = "AAZZBBZB";

    char[] chars = document.ToCharArray();

 

    CharacterFactory factory = new CharacterFactory();

 

    Int32 pointSize = 10;

 

    foreach(char c in chars)

    {

        pointSize++;

 

        Character character = factory[c];

        character.Display(pointSize);

    }

}

 

main.cs

 

using System;

 

namespace DesignPatterns.Structural_Patterns.Flyweight

{

    public abstract class Character

    {

        protected char Symbol { get; set; }

        protected Int32 Width { get; set; }

        protected Int32 Height { get; set; }

        protected Int32 Ascent { get; set; }

        protected Int32 Descent { get; set; }

        protected Int32 PointSize { get; set; }

 

        public abstract void Display(Int32 pointSize);

    }

}

 

Character.cs

 

using System;

 

namespace DesignPatterns.Structural_Patterns.Flyweight.Characters

{

    public class CharacterA : Character

    {

        public CharacterA()

        {

            this.Symbol = 'A';

            this.Height = 100;

            this.Width = 120;

            this.Ascent = 70;

            this.Descent = 0;

        }

 

        public override void Display(int pointSize)

        {

            this.PointSize = pointSize;

 

            Console.WriteLine(string.Format("{0} (point size : {1})"this.Symbol, pointSize));

        }

    }

}

 

CharacterA.cs

 

using System;

 

namespace DesignPatterns.Structural_Patterns.Flyweight.Characters

{

    public class CharacterB : Character

    {

        public CharacterB()

        {

            this.Symbol = 'B';

            this.Height = 100;

            this.Width = 140;

            this.Ascent = 72;

            this.Descent = 0;

        }

 

        public override void Display(int pointSize)

        {

            this.PointSize = pointSize;

 

            Console.WriteLine(string.Format("{0} (point size : {1})"this.Symbol, pointSize));

        }

    }

}

 

CharacterB.cs

 

using System;

 

namespace DesignPatterns.Structural_Patterns.Flyweight.Characters

{

    public class CharacterZ : Character

    {

        public CharacterZ()

        {

            this.Symbol = 'Z';

            this.Height = 100;

            this.Width = 100;

            this.Ascent = 68;

            this.Descent = 0;

        }

 

        public override void Display(int pointSize)

        {

            this.PointSize = pointSize;

 

            Console.WriteLine(string.Format("{0} (point size : {1})"this.Symbol, pointSize));

        }

    }

}

 

CharacterZ.cs

 

using System;

using System.Collections.Generic;

 

namespace DesignPatterns.Structural_Patterns.Flyweight

{

    public class CharacterFactory

    {

        public Character this[char key]

        {

            get

            {

                return GetCharacter(key);

            }

        }

 

        private Dictionary<char, Character> characters = new Dictionary<char, Character>();

        private readonly string NameSapcePath = "DesignPatterns.Structural_Patterns.Flyweight.Characters.Character";

 

        private Character GetCharacter(char key)

        {

            if (characters.TryGetValue(key, out Character character) != true)

            {

                Type t = Type.GetType(string.Format("{0}{1}", NameSapcePath, key));

                character = (Character)Activator.CreateInstance(t);

                characters.Add(key, character);

            }

 

            return character;

        }

    }

}

 

CharacterFactory.cs

 

참고 사이트

https://www.dofactory.com/net/flyweight-design-pattern