05 Dec 2023



Intermediate

The Flyweight Design Pattern is a structural pattern in software design that aims to minimize memory usage and improve performance by sharing and reusing objects. It involves creating lightweight objects (flyweights) that store common and intrinsic state externally, while keeping the unique and extrinsic state within each instance. This pattern is particularly useful when a large number of similar objects need to be created, as it reduces the overall memory footprint by sharing commonalities among multiple instances.

Note📝:

  • Intrinsic: Essential or inherent characteristics that are shared among multiple objects. These are properties that remain constant across instances.
  • Extrinsic: Non-essential or external characteristics that are specific to each instance. These are properties that can vary from one instance to another.

Key points of the Flyweight Design Pattern:

  1. Intent:

    • The main intent of the Flyweight pattern is to use shared objects to support a large number of fine-grained objects efficiently. It aims to minimize memory usage or computational expenses by sharing as much as possible between related objects.
  2. Key Components:

    • Flyweight:
      • Declares an interface through which flyweights can receive and act on extrinsic state. It is the common interface for all concrete flyweights.
    • ConcreteFlyweight:
      • Implements the Flyweight interface and represents concrete flyweight objects. Instances of concrete flyweights are shared and can be reused.
    • UnsharedConcreteFlyweight:
      • Represents flyweights that cannot be shared. While it still implements the Flyweight interface, instances of unshared concrete flyweights are not shared among clients.
    • FlyweightFactory:
      • Manages a pool of flyweight objects, ensuring that flyweights are shared and reused when possible.
  3. Intrinsic and Extrinsic State:

    • Intrinsic State: Information that can be shared and is stored in the flyweight. It is independent of the context in which the flyweight is used.
    • Extrinsic State: Information that cannot be shared and must be passed to the flyweight when it is used. It depends on the context.
  4. Shared and Non-shared Objects:

    • Flyweight objects can be shared (concrete flyweights) or non-shared (unshared concrete flyweights).
  5. Advantages:

    • Memory Efficiency: It reduces memory usage by sharing common parts of objects.
    • Performance Improvement: It improves performance by reusing shared flyweights instead of creating new ones.
    • Flexibility: It allows for both shared and non-shared objects to coexist in the same system.
  6. Use Cases:

    • The Flyweight pattern is suitable when a large number of similar objects need to be managed efficiently.
    • It is applicable when the shared state among objects can be extracted and when the extrinsic state can be passed to the objects.
  7. Example:

    • Consider a text editor where each character in a document is represented as a flyweight. Common characters, such as space or punctuation, are shared flyweights, while unique characters, such as individual letters, are non-shared flyweights.
software-design-patterns
flyweight-design-pattern