The Flyweight Design Pattern

The Flyweight pattern minimizes memory usage or computational expenses by sharing as much as possible with related objects.

Use the Flyweight pattern when you need to improve the efficiency and performance of an object-heavy application by sharing object data.

  • TypeStructural
  • Time ComplexityO(n) Linear Time – The time taken grows linearly with the size of the input. Examples include simple search algorithms.
  • Efficiency9
  • Learning Effort

    • Average6
    • Conceptual7
    • Debug and Maintain6
    • Implementation8
    • Prerequisites6
    • Versatility5

Things you should know before you start

  • Object Pooling Basic understanding of object pooling for resource management.
  • Immutability Understanding of immutability as flyweights are often immutable.

Minimal Example

Optimizing a text editor that handles large text files by sharing character objects.

class Character {
  constructor(private char: string) {}
  public display() {
    console.log(this.char);
  }
}
const characterFactory = new CharacterFactory();
const charA1 = characterFactory.getCharacter('A');
const charA2 = characterFactory.getCharacter('A');
charA1.display();
charA2.display();

What’s happening in thie example

In this example, the `Character` class is the Flyweight that shares character data. The `clientCode` shows how two variables (`charA1` and `charA2`) share the same character object.

How the Flyweight Works

The Flyweight pattern optimizes code by sharing the data intrinsic to objects; it’s used in scenarios where a large number of similar objects are used that have only a few fields with varying information.

This pattern is particularly useful in systems where the reduction of memory footprint significantly impacts performance.

Entities in the Flyweight Design Pattern

  • FlyweightThe Flyweight interface enables sharing but defers the input-output operations to the client object.

    • Flyweight has a one-to-many inherits relationship with ConcreteFlyweight

What to watch out for when using or considering this design pattern.

Avoid using the Flyweight pattern when the shared objects carry a lot of mutable state, as this can introduce complexity and bugs.

Design patterns often confused with the Flyweight Design Pattern

The following design patterns are often confused with the Flyweight
for various resons.

If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.

  • Singleton – The Singleton pattern ensures that a class has only one instance and provides a global point of access to it.

    The Singleton pattern is similar to the Flyweight in the following ways.

    • Both aim to control object creation.
    • Both are used to improve system performance.
  • Here are some notable differences with the Flyweight pattern.

    • Flyweight allows multiple instances but shares intrinsic data, whereas Singleton restricts object creation to one instance.
    • Flyweight focuses on sharing data to minimize memory usage, while Singleton focuses on having a single point of access to an instance.
  • Prototype – The Prototype pattern creates new objects by copying an existing object, known as the prototype.

    The Prototype pattern is similar to the Flyweight in the following ways.

    • Both patterns aim to improve performance.
    • Both deal with object creation.
  • Here are some notable differences with the Flyweight pattern.

    • Flyweight focuses on sharing object data to minimize memory usage, while Prototype focuses on avoiding the cost of object creation by cloning objects.
    • Flyweight objects are usually immutable, whereas Prototypes can be modified after cloning.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top