loader

Ever sent the same emoji over and over in your messages and wondered how your app doesn’t explode with memory usage? That’s the magic of the Flyweight Pattern! It helps you reuse existing objects instead of creating new ones every time—just like using your favorite emoji again and again without duplicating it in memory.

Why Bother with It?

  • Memory Efficiency: Instead of creating a new emoji object for every message, you reuse a single instance. This saves memory—especially when you’re sending a ton of the same emoji.
  • Speed Boost: By reusing objects, your application runs faster because it avoids the overhead of object creation.
  • Simpler Management: It’s easier to manage and update one shared object than to keep track of hundreds or thousands of duplicates.

How It Works (In Plain English)

  1. Common Shared Object: Imagine you have one gold-standard emoji that represents “?”. Every time you need to show that smile, you just reference the same emoji.
  2. The Factory: A factory class checks if the emoji already exists. If it does, it hands you the existing one; if not, it creates it.
  3. External Context: While the emoji itself is shared (its intrinsic state), any additional information (like the message sender, timestamp, etc.) is kept outside the emoji. This extra info is the extrinsic state.
  4. Reuse and Save: Each time the emoji is needed, you reuse the shared instance and only add in the specific details needed for that moment.

C# Code Example

C#
using System;
using System.Collections.Generic;

// Flyweight Interface
public interface IEmoji
{
    void Display(string context);
}

// Concrete Flyweight: Represents a shared Emoji
public class Emoji : IEmoji
{
    public string Symbol { get; private set; }
    
    public Emoji(string symbol)
    {
        Symbol = symbol;
    }
    
    public void Display(string context)
    {
        Console.WriteLine($"Emoji: {Symbol} displayed in context: {context}");
    }
}

// Flyweight Factory: Manages and reuses emoji instances
public class EmojiFactory
{
    private Dictionary<string, IEmoji> _emojiCache = new Dictionary<string, IEmoji>();
    
    public IEmoji GetEmoji(string symbol)
    {
        if (!_emojiCache.ContainsKey(symbol))
        {
            _emojiCache[symbol] = new Emoji(symbol);
        }
        return _emojiCache[symbol];
    }
}

// Client Code
public class Program
{
    public static void Main()
    {
        EmojiFactory factory = new EmojiFactory();
        
        IEmoji smileEmoji = factory.GetEmoji("?");
        IEmoji laughEmoji = factory.GetEmoji("?");
        
        // Display emojis in different contexts
        smileEmoji.Display("Chat with Alice");
        smileEmoji.Display("Chat with Bob");
        laughEmoji.Display("Group Chat");
        
        // Verify reuse of the same instance
        IEmoji anotherSmile = factory.GetEmoji("?");
        Console.WriteLine(object.ReferenceEquals(smileEmoji, anotherSmile)
            ? "Same emoji instance reused!"
            : "Different instance created!");
    }
}

The Takeaway

The Flyweight Pattern is like having one trusty emoji that you share across all your messages. By reusing the same object for common elements, you save memory, boost performance, and simplify management in your code. So next time you’re about to send that “?” again, remember—Flyweight’s got your back!

Happy coding, and keep those emojis flying!