Skip to content

Files

Latest commit

79a4483 · Jan 6, 2025

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Flyweight Pattern

The Flyweight Pattern is a structural design pattern that minimizes memory usage by sharing common parts of state between multiple objects.


📖 What is the Flyweight Pattern?

The Flyweight Pattern reduces memory usage by sharing as much data as possible between similar objects. This is achieved by separating intrinsic (shared) and extrinsic (unique) states.

Key features:

  1. Shared State: Intrinsic state is shared between objects.
  2. Reduced Memory Usage: Minimize memory overhead by reusing existing objects.
  3. Performance Optimization: Suitable for large numbers of similar objects.

🤔 Why Use the Flyweight Pattern?

  1. Memory Efficiency: Reduces memory consumption for large numbers of objects.
  2. Reusability: Encourages reuse of shared states.
  3. Performance: Optimizes performance in scenarios with many similar objects.

🔧 Implementation

The implementation of the Flyweight Pattern can be found in:


🛠️ Example Usage

To see the Flyweight Pattern in action, refer to the Main.java file. It demonstrates how to reuse shapes with shared properties.


📊 UML Diagram

Loading
classDiagram
direction LR
    class Client {
    }

    class Flyweight {
        +method(extrinsicState)
    }

    class FlyweightFactory {
        +getID(key)
        +cache: Flyweight[]
    }

    class SharedFlyweight {
        -intrinsicState1
        -intrinsicState2
        +method(extrinsicState)
    }

    class UnsharedFlyweight {
        -intrinsicState1
        -intrinsicState2
        -extrinsicState1
        -extrinsicState2
        +method(extrinsicState)
    }

    Client --> Flyweight : Flyweight
    Flyweight <|-- SharedFlyweight
    Flyweight <|-- UnsharedFlyweight
    Client --> FlyweightFactory
    FlyweightFactory --> SharedFlyweight : << create & share >>


Note

If the UML above is not rendering correctly, you can view the diagram from the flyweight_uml.png file.


📝 Key Takeaways

  • The Flyweight Pattern is ideal for optimizing memory usage when dealing with many similar objects.
  • Use it when you need a large number of objects that share most of their state.
  • It reduces memory overhead by sharing intrinsic state between objects.