Flyweight — share common state.
Implementation:
1public class CharacterFlyweight2{3 private readonly char _symbol;4 private readonly Font _font;56 public CharacterFlyweight(char symbol, Font font)7 {8 _symbol = symbol;9 _font = font;10 }1112 public void Draw(int x, int y) =>13 Console.WriteLine($"Draw {_symbol} at ({x},{y})");14}1516public class FlyweightFactory17{18 private readonly Dictionary<char, CharacterFlyweight> _cache = new();1920 public CharacterFlyweight Get(char c)21 {22 if (!_cache.ContainsKey(c))23 _cache[c] = new CharacterFlyweight(c, new Font());24 return _cache[c];25 }26}
Key: Flyweight for memory optimization.