Flyweight Pattern in C#
The Flyweight Pattern helps you save memory by sharing common parts of objects instead of creating duplicates. It is especially useful when you need to create thousands or millions of similar objects.
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.

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
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.
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);
}
}
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.