Flyweight Pattern: Airline Seat Map

The Flyweight Pattern is used to save memory by sharing objects that are similar. When you have many objects that are almost the same, you only store the unique parts, and share the rest.

This is useful in systems that deal with lots of small data, like text editors or games. It helps your software use less memory and run faster.

Real-Life Analogy: Airline Seat Map

Imagine you're looking at an airline's seating chart for a big flight. Each seat is shown with the same shape, size, and base style — just in a different position. Instead of creating a unique image for every seat, the system uses one reusable seat design and places it at different coordinates.

This saves memory and speeds things up. That's what the Flyweight Pattern does in your code — it shares what can be shared and stores unique data separately.

A seat map showing repeated seat icons drawn from the same template
Like using one seat template for an entire airline seat map, the Flyweight Pattern saves memory by reusing shared data.

Benefits of Flyweight Pattern

When you create many similar objects, memory and performance become important. The Flyweight Pattern lets you separate the part that repeats from the part that's unique — and reuse what you can.

This pattern is most useful when there are huge numbers of small objects with common data.

  • Reduces memory usage by reusing shared parts
  • Improves performance when creating many similar objects
  • Separates intrinsic (shared) and extrinsic (unique) state

What to Implement

To implement the Flyweight Pattern, you need:

A shared object that contains reusable data, and a factory that returns shared instances. You also need to separate what can't be shared (like position).

  • Flyweight: Contains shared state
  • Concrete Flyweight: Implements the shared logic
  • Flyweight Factory: Returns existing flyweights or creates new ones
  • Client: Supplies unique state during use

How It Works in C#


// Flyweight
public interface ISeat
{
    void Draw(int row, char column);
}

// Concrete Flyweight
public class EconomySeat : ISeat
{
    public void Draw(int row, char column)
    {
        Console.WriteLine($""Drawing Economy Seat at {row}{column}"");
    }
}

// Flyweight Factory
public class SeatFactory
{
    private readonly Dictionary<string, ISeat> seats = new();

    public ISeat GetSeat(string type)
    {
        if (!seats.ContainsKey(type))
        {
            if (type == ""Economy"")
                seats[type] = new EconomySeat();
        }

        return seats[type];
    }
}

Usage:


var factory = new SeatFactory();
for (int row = 1; row <= 3; row++)
{
    foreach (var col in new[] {{ 'A', 'B', 'C' }})
    {
        ISeat seat = factory.GetSeat(""Economy"");
        seat.Draw(row, col);
    }
}

When Should You Use It?

Use the Flyweight Pattern when your app needs to manage large numbers of similar objects and memory is a concern. It works best when most of the data is the same and only a few parts differ.

It fits well when:

  • Objects have repeating, sharable data
  • You want to avoid memory duplication
  • The cost of creating and storing many objects is too high

When Not to Use It: Avoid it if your objects are large and completely different — the extra complexity won't help.

Where Is It Used in the Real World?

The Flyweight Pattern is essential when memory is a concern and objects share a lot of data. It is used in graphics, document editors, and caching.

  • Text rendering (one character object per glyph, shared across text)
  • Game development (tiles, sprites, and repeating objects)
  • Document editors (formatting runs, shared styles)
  • Caching systems (object pools, connection pools)

Final Thoughts

The Flyweight Pattern is about efficiency. When you need to display, manage, or store lots of similar things — like map tiles, text characters, or seats — this pattern saves space and boosts performance.

It's perfect when memory counts, and many objects look alike.