design-patternsflyweight-pattern

Flyweight pattern dilemma


This Flyweight DP tutorial shows a good explanation of the Flyweight pattern.

Anyway, let's say that we want to change the font size of the this.height = 200; (assume that the properties are not protected). That means that all the font sizes will be changed since objects are of reference type.

Let's say that this is a text editor, and we want to change the font size only for a specific letter (single instance of the letter, not all). So now a problem occurs since if we somehow change the font size of the object in the dictionary, all of the occurrences will be changed. How can I deal with this?


Solution

  • I think this is a solution that might fit. It is a flyweight inside a flyweight, but in C#, it is a little bit simplified but I think you should get the concept. The idea is to put a facade in place to get the parametrized classes. Inside that facade i have a dictionary that makes new factories for new fontsizes. This might seem weird, but beware that the flyweight is a creational design pattern, it will not store the text itself, only give you access to the same objects. And the state is obviously not the same, so i think a different factory might be justifiable.

        public class AlphabetFacade
        {
            private Dictionary<int, AlphabetFactory> _alphabetFactories = new Dictionary<int, AlphabetFactory>();
    
            private Character GetChar(string character, int fontSize)
            {
                if (_alphabetFactories.ContainsKey(fontSize)) return _alphabetFactories[fontSize].GetCharacter(character);
                var factoryNew = new AlphabetFactory(fontSize);
                _alphabetFactories.Add(fontSize, factoryNew);
                return factoryNew.GetCharacter(character);
            }
        }
    
        internal class AlphabetFactory
        {
            private readonly int _fontSize;
    
            private readonly Dictionary<string, Character> _characters = new Dictionary<string, Character>();
    
            public AlphabetFactory(int fontSize)
            {
                _fontSize = fontSize;
            }
    
            public Character GetCharacter(string character)
            {
                if (_characters.ContainsKey(character)) return _characters[character];
                var characterNew = new Character(_fontSize, character);
                _characters.Add(character, characterNew);
                return characterNew;
            }
        }
    
         class Character
         {
             public Character(int size, string @char)
             {
                Size = size;
                Char = @char;
             }
    
             public int Size { get; }
             public string Char { get; }
         }