FlyWeight Design Pattern in C#

The Flyweight design pattern is a structural pattern used to reduce memory usage or computational costs by sharing as much as possible with related objects. It is particularly useful when you have a large number of similar objects and want to minimize the overhead of creating and storing individual instances of those objects. The Flyweight pattern separates the intrinsic (shared) state from the extrinsic (unique) state of an object.

Let’s create an example of the Flyweight pattern in C# with a use case involving a text editor where we store and reuse character objects efficiently.

Define the Flyweight interface that all concrete flyweights must implement

public interface ICharacter
{
    void Display(char character);
}

Create a concrete flyweight class that represents the shared character object (intrinsic state)

public class Character : ICharacter
{
    private char symbol;

    public Character(char symbol)
    {
        this.symbol = symbol;
    }

    public void Display(char character)
    {
        Console.WriteLine($"Character {symbol} with character code {((int)symbol)}.");
    }
}

Create a Flyweight Factory that manages the shared flyweight objects and returns them when requested. This factory ensures that shared objects are reused and that new ones are created only when necessary

public class CharacterFactory
{
    private Dictionary<char, ICharacter> characters = new Dictionary<char, ICharacter>();

    public ICharacter GetCharacter(char symbol)
    {
        if (!characters.ContainsKey(symbol))
        {
            characters[symbol] = new Character(symbol);
        }
        return characters[symbol];
    }
}

Use the Flyweight pattern in a client class to create and display characters efficiently

class TextEditor
{
    private CharacterFactory characterFactory;

    public TextEditor(CharacterFactory characterFactory)
    {
        this.characterFactory = characterFactory;
    }

    public void DisplayText(string text)
    {
        foreach (char character in text)
        {
            ICharacter flyweight = characterFactory.GetCharacter(character);
            flyweight.Display(character);
        }
    }
}

Demonstrate how the Flyweight pattern optimizes the storage and display of characters in a text editor

class Program
{
    static void Main(string[] args)
    {
        CharacterFactory characterFactory = new CharacterFactory();
        TextEditor editor = new TextEditor(characterFactory);

        string text = "Flyweight Pattern Example";
        editor.DisplayText(text);
    }
}

In this example, the Flyweight pattern is used to efficiently store and display characters in a text editor. The CharacterFactory ensures that characters are created and shared efficiently, and the TextEditor uses these characters to display text. This pattern is beneficial when dealing with a large number of similar objects and helps reduce memory consumption and improve performance.