/// <summary>
        /// The get character.
        /// </summary>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <returns>
        /// The <see cref="AbstractCharacter"/>.
        /// </returns>
        public AbstractCharacter GetCharacter(char key)
        {
            // Uses "lazy initialization"
            AbstractCharacter character = null;

            if (this.characters.ContainsKey(key))
            {
                character = this.characters[key];
            }
            else
            {
                switch (key)
                {
                case 'A': character = new CharacterA(); break;

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

                //...
                case 'Z': character = new CharacterZ(); break;
                }
                this.characters.Add(key, character);
            }

            return(character);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The test first.
        /// </summary>
        private static void TestFirst()
        {
            // Build a document with text
            const string Document = "AAZZBBZB";

            char[]           chars            = Document.ToCharArray();
            CharacterFactory characterFactory = new CharacterFactory();

            // extrinsic state
            int pointSize = 10;

            // For each character use a flyweight object
            foreach (char c in chars)
            {
                pointSize++;
                AbstractCharacter abstractCharacter = characterFactory.GetCharacter(c);
                abstractCharacter.Display(pointSize);
            }

            // Wait for user
            Console.ReadKey();
        }