What is the Flyweight Design Pattern?

Göksu Deniz
10 min readMay 7, 2023

--

Image is created by DALL-E 2 AI

The flyweight pattern is a structural design pattern that is used to minimize memory usage by sharing data that is common to multiple objects. The pattern is based on the idea that certain parts of an object can be shared among multiple objects, rather than being duplicated for each individual object. By sharing common data, the overall memory usage of the program is reduced, which can lead to improved performance and scalability.

To understand how the flyweight pattern works, let’s consider an example. Imagine that you are building a word processing application that allows users to create and edit documents. Each document contains a large amount of text, which can be divided into individual paragraphs. Each paragraph may have its own style (such as font, size, and color), but many paragraphs may share the same style. Since there may be many paragraphs in a document, and many documents open at once, it is important to minimize memory usage and improve performance wherever possible.

To implement the flyweight pattern in this scenario, we can create a ParagraphStyle class to represent the shared data (i.e., the style properties of a paragraph). We can also create a Paragraph class that references a ParagraphStyle object, rather than duplicating the style properties for each individual paragraph.

By using the flyweight pattern, we can significantly reduce the memory usage of our program, since we are only storing the common data once. Additionally, since the flyweight objects are immutable (i.e., they cannot be changed once they are created), we can safely share them among multiple objects without worrying about unintended side effects.

To implement the flyweight pattern in your own programs, you will typically need to do the following:

  1. Identify the data that can be shared among multiple objects.
  2. Create a flyweight object to represent the shared data.
  3. Modify your existing objects to reference the flyweight object instead of duplicating the shared data.
  4. Create new objects by using the flyweight object as a template.

Of course, the flyweight pattern is not a silver bullet, and it may not be appropriate for all situations. In some cases, the overhead of creating and managing flyweight objects may actually outweigh the benefits of reduced memory usage. Additionally, if the shared data is frequently updated or modified, the flyweight pattern may not be appropriate, since it relies on the immutability of the flyweight objects.

// The flyweight object representing the shared data
class ParagraphStyle
{
public string Font { get; set; }
public int Size { get; set; }
public string Color { get; set; }
}

// The context object that references the flyweight object
class Paragraph
{
private string _text;
private ParagraphStyle _style;

public Paragraph(string text, ParagraphStyle style)
{
_text = text;
_style = style;
}

public string Text
{
get { return _text; }
}

public string Font
{
get { return _style.Font; }
}

public int Size
{
get { return _style.Size; }
}

public string Color
{
get { return _style.Color; }
}
}

// The flyweight factory that manages the creation of flyweight objects
class ParagraphStyleFactory
{
private Dictionary<string, ParagraphStyle> _styles = new Dictionary<string, ParagraphStyle>();

public ParagraphStyle GetStyle(string font, int size, string color)
{
string key = $"{font}-{size}-{color}";

if (!_styles.ContainsKey(key))
{
_styles[key] = new ParagraphStyle { Font = font, Size = size, Color = color };
}

return _styles[key];
}
}

// Usage example
ParagraphStyleFactory factory = new ParagraphStyleFactory();
ParagraphStyle sharedStyle = factory.GetStyle("Arial", 12, "Black");

Paragraph paragraph1 = new Paragraph("This is the first paragraph.", sharedStyle);
Paragraph paragraph2 = new Paragraph("This is the second paragraph.", sharedStyle);

Console.WriteLine(paragraph1.Font); // Outputs "Arial"
Console.WriteLine(paragraph2.Color); // Outputs "Black"

In this example, we have a ParagraphStyle class that represents the shared data (i.e., the font, size, and color of a paragraph). We also have a Paragraph class that references a ParagraphStyle object, rather than duplicating the style properties itself. Finally, we have a ParagraphStyleFactory class that manages the creation of ParagraphStyle objects and ensures that the same object is reused whenever possible.

To create a new Paragraph object, we first obtain a ParagraphStyle object from the ParagraphStyleFactory. If a ParagraphStyle object with the same font, size, and color already exists, the factory will return that object. Otherwise, it will create a new object and return that.

Note that in this example, the ParagraphStyle object is mutable (i.e., it has public setters for its properties). This is not always the case in a flyweight pattern, but it can be appropriate in certain situations. If the shared data is mutable, you will need to take care to ensure that any changes made to the shared data are synchronized among all objects that reference it. This may involve creating a separate "context" object for each object that references the flyweight object, to encapsulate the mutable state. Alternatively, you may need to use some form of locking or synchronization to ensure that only one thread at a time is modifying the shared data.

Here are some real-world scenarios where the Flyweight pattern can be useful:

  1. Text editing applications: In text editing applications, there are often a large number of characters and glyphs that need to be displayed on the screen. Instead of creating a new object for each character or glyph, the Flyweight pattern can be used to create a shared flyweight object for each distinct character or glyph, and reuse it across multiple text elements.
  2. Financial applications: In financial applications, there may be a large number of financial instruments with similar characteristics, such as stocks, bonds, and options. The Flyweight pattern can be used to create a shared flyweight object for each distinct financial instrument type, and reuse it across multiple financial calculations.
  3. Web applications: In web applications, there may be a large number of user interface elements that need to be rendered on the screen, such as buttons, icons, and form elements. The Flyweight pattern can be used to create a shared flyweight object for each distinct UI element type, and reuse it across multiple pages.
  4. Games: In games, there may be a large number of objects with similar behavior and appearance, such as enemies, projectiles, and power-ups. The Flyweight pattern can be used to create a shared flyweight object for each distinct object type, and reuse it across multiple instances.
  5. Geospatial applications: In geospatial applications, there may be a large number of geographic features with similar properties, such as roads, rivers, and buildings. The Flyweight pattern can be used to create a shared flyweight object for each distinct feature type, and reuse it across multiple map instances.

These are just a few examples of real-world scenarios where the Flyweight pattern can be useful. Any application that involves creating a large number of objects with similar properties can potentially benefit from the Flyweight pattern.

Here’s an example implementation of the Flyweight pattern in C# for a financial application:

using System;
using System.Collections.Generic;

// Flyweight interface
public interface IFinancialInstrument
{
decimal CalculatePrice(DateTime date, decimal strikePrice);
}

// Concrete flyweight class
public class Stock : IFinancialInstrument
{
private readonly string _symbol;
public Stock(string symbol)
{
_symbol = symbol;
Console.WriteLine($"Stock '{_symbol}' created");
}

public decimal CalculatePrice(DateTime date, decimal strikePrice)
{
// Perform financial calculations for stock
return 100.00m;
}
}

// Flyweight factory class
public class FinancialInstrumentFactory
{
private readonly Dictionary<string, IFinancialInstrument> _instruments = new Dictionary<string, IFinancialInstrument>();

public int TotalInstrumentsCreated
{
get { return _instruments.Count; }
}

public IFinancialInstrument GetInstrument(string symbol)
{
IFinancialInstrument instrument = null;
if (_instruments.ContainsKey(symbol))
{
instrument = _instruments[symbol];
}
else
{
instrument = new Stock(symbol);
_instruments.Add(symbol, instrument);
}
return instrument;
}
}

// Client code
public class Client
{
private readonly FinancialInstrumentFactory _instrumentFactory;
public Client(FinancialInstrumentFactory instrumentFactory)
{
_instrumentFactory = instrumentFactory;
}

public void CalculatePrices()
{
IFinancialInstrument stock1 = _instrumentFactory.GetInstrument("AAPL");
decimal price1 = stock1.CalculatePrice(DateTime.Today, 110.00m);
Console.WriteLine($"Price of AAPL stock: {price1:C}");

IFinancialInstrument stock2 = _instrumentFactory.GetInstrument("AAPL");
decimal price2 = stock2.CalculatePrice(DateTime.Today, 120.00m);
Console.WriteLine($"Price of AAPL stock: {price2:C}");

IFinancialInstrument stock3 = _instrumentFactory.GetInstrument("GOOG");
decimal price3 = stock3.CalculatePrice(DateTime.Today, 130.00m);
Console.WriteLine($"Price of GOOG stock: {price3:C}");

Console.WriteLine("Total number of financial instruments created: " + _instrumentFactory.TotalInstrumentsCreated);
}
}

// Usage
public static void Main()
{
FinancialInstrumentFactory instrumentFactory = new FinancialInstrumentFactory();
Client client = new Client(instrumentFactory);
client.CalculatePrices();
}

In this example, the Stock class is the concrete flyweight class, which implements the IFinancialInstrument interface. The FinancialInstrumentFactory class is the flyweight factory class, which manages the creation and sharing of flyweight objects. The Client class is the client code that uses the flyweight objects.

When the GetInstrument method of the FinancialInstrumentFactory class is called with a symbol, it checks if a flyweight object of that symbol already exists in the _instruments dictionary. If it does, it returns that object. If not, it creates a new flyweight object of that symbol, adds it to the _instruments dictionary, and returns it.

In the CalculatePrices method of the Client class, three flyweight objects are created using the GetInstrument method of the FinancialInstrumentFactory class. The first and second objects share the same symbol ("AAPL"), while the third object has a different symbol ("GOOG"). The intrinsic state of each financial instrument object (i.e. financial calculations) is used to calculate the prices for each instrument. Finally, the total number of financial instruments created is printed to the console.

This example is a simplified scenario for a financial application, where the intrinsic state of each financial instrument object is its financial calculations. In a real-world financial application, the intrinsic state of each object could be much more complex, such as historical price data, market volatility, or option strike prices. The Flyweight pattern can be useful in such scenarios to minimize memory usage and improve performance by sharing the intrinsic state across multiple objects.

There can be multiple concrete classes in the Flyweight pattern. The key idea behind the Flyweight pattern is to share objects that have the same intrinsic state to reduce memory usage and improve performance. This can be achieved by creating a separate Flyweight interface that defines the common methods for the flyweight objects, and then implementing this interface in one or more concrete flyweight classes.

The number of concrete classes depends on the complexity of the intrinsic state and the number of distinct intrinsic states that need to be represented. If the intrinsic state is relatively simple, such as a single string or integer value, then a single concrete class may be sufficient. However, if the intrinsic state is more complex, such as a combination of multiple values or a reference to a large data structure, then multiple concrete classes may be necessary to represent different combinations of intrinsic state.

For example, in a game where the intrinsic state of an enemy object consists of its appearance, health, and attack power, there could be multiple concrete classes that represent different combinations of these properties. One concrete class could represent weak enemies with low health and attack power, while another concrete class could represent strong enemies with high health and attack power. Both classes would implement the same Flyweight interface to ensure that they can be used interchangeably in the game.

using System;
using System.Collections.Generic;

// Flyweight interface
public interface IEnemy
{
void Attack();
}

// Concrete flyweight class for weak enemies
public class WeakEnemy : IEnemy
{
private readonly string _appearance = "Weak Enemy";
private readonly int _health = 10;
private readonly int _attackPower = 1;

public void Attack()
{
// Perform attack action for weak enemy
Console.WriteLine($"{_appearance} attacks for {_attackPower} damage");
}
}

// Concrete flyweight class for strong enemies
public class StrongEnemy : IEnemy
{
private readonly string _appearance = "Strong Enemy";
private readonly int _health = 50;
private readonly int _attackPower = 5;

public void Attack()
{
// Perform attack action for strong enemy
Console.WriteLine($"{_appearance} attacks for {_attackPower} damage");
}
}

// Flyweight factory class
public class EnemyFactory
{
private readonly Dictionary<string, IEnemy> _enemies = new Dictionary<string, IEnemy>();

public int TotalEnemiesCreated
{
get { return _enemies.Count; }
}

public IEnemy GetEnemy(string type)
{
IEnemy enemy = null;
if (_enemies.ContainsKey(type))
{
enemy = _enemies[type];
}
else
{
switch (type)
{
case "weak":
enemy = new WeakEnemy();
break;
case "strong":
enemy = new StrongEnemy();
break;
default:
throw new ArgumentException($"Invalid enemy type '{type}'");
}
_enemies.Add(type, enemy);
}
return enemy;
}
}

// Client code
public class Client
{
private readonly EnemyFactory _enemyFactory;
public Client(EnemyFactory enemyFactory)
{
_enemyFactory = enemyFactory;
}

public void Battle()
{
IEnemy enemy1 = _enemyFactory.GetEnemy("weak");
enemy1.Attack();

IEnemy enemy2 = _enemyFactory.GetEnemy("weak");
enemy2.Attack();

IEnemy enemy3 = _enemyFactory.GetEnemy("strong");
enemy3.Attack();

Console.WriteLine("Total number of enemies created: " + _enemyFactory.TotalEnemiesCreated);
}
}

// Usage
public static void Main()
{
EnemyFactory enemyFactory = new EnemyFactory();
Client client = new Client(enemyFactory);
client.Battle();
}

In this example, there are two concrete flyweight classes: WeakEnemy and StrongEnemy, both of which implement the IEnemy interface. The EnemyFactory class is the flyweight factory class, which manages the creation and sharing of flyweight objects. The Client class is the client code that uses the flyweight objects.

When the GetEnemy method of the EnemyFactory class is called with a type, it checks if a flyweight object of that type already exists in the _enemies dictionary. If it does, it returns that object. If not, it creates a new flyweight object of that type, adds it to the _enemies dictionary, and returns it.

In the Battle method of the Client class, three flyweight objects are created using the GetEnemy method of the EnemyFactory class. The first and second objects share the same type ("weak"), while the third object has a different type ("strong"). The intrinsic state of each enemy object (i.e. appearance, health, and attack power) is used to perform the attack action for each enemy. Finally, the total number of enemies created is printed to the console.

This example is a simplified scenario for a game, where the intrinsic state of each enemy object is its appearance, health, and attack power. In a real-world game, the intrinsic state of each object could be much more complex, such as AI behavior, weapon type, or armor level. The Flyweight pattern can be useful in such scenarios to minimize memory usage and improve performance by sharing the intrinsic state across multiple objects.

Conclusion

In conclusion, the Flyweight pattern is a useful design pattern for minimizing memory usage and improving performance in situations where many objects need to be created, but share a significant amount of common state. By separating intrinsic and extrinsic state and sharing intrinsic state among objects, the Flyweight pattern can greatly reduce memory consumption, increase efficiency, and improve scalability.

In addition, the Flyweight pattern can be used in a variety of scenarios, including financial applications, web applications, games, and many others. The key to successfully implementing the Flyweight pattern is to identify the common and variable properties of the objects, and to separate the intrinsic and extrinsic state accordingly. By doing so, the Flyweight pattern can help to optimize application performance and memory usage, making it a valuable tool for software developers.

Thanks for reading! If you found the article helpful, you can clap and follow. So you will notified of new articles.

# Reference

It was craeted with the help of ChatGPT AI.

--

--

Göksu Deniz
Göksu Deniz

Written by Göksu Deniz

Software Engineer, passionate about creating efficient solutions. Skilled in mentoring teams to deliver successful projects. Always exploring new tech trends.

No responses yet