Key Component of Flyweight Design Patterns

Flyweight Interface/Abstract Class: Define an interface or an abstract class that declares the methods that must be implemented by concrete flyweight objects. This interface usually represents the common functionality shared among the flyweight objects.

Java




public interface Flyweight {
    void operation();
}


Concrete Flyweight: Implement the Flyweight interface to create concrete flyweight objects. These objects are shared and can be used in multiple contexts.

Java




public class ConcreteFlyweight implements Flyweight {
    private String intrinsicState;
 
    public ConcreteFlyweight(String intrinsicState) {
        this.intrinsicState = intrinsicState;
    }
 
    @Override
    public void operation() {
        System.out.println("ConcreteFlyweight: " + intrinsicState);
    }
}


Flyweight Factory: Create a factory class that manages the creation and sharing of flyweight objects. The factory maintains a pool or cache of existing flyweight objects and returns them when requested.

Java




import java.util.HashMap;
import java.util.Map;
 
public class FlyweightFactory {
    private Map<String, Flyweight> flyweights = new HashMap<>();
 
    public Flyweight getFlyweight(String key) {
        if (!flyweights.containsKey(key)) {
            flyweights.put(key, new ConcreteFlyweight(key));
        }
        return flyweights.get(key);
    }
}


Client: The client is responsible for using the flyweight objects. It typically maintains the extrinsic state, which is unique to each instance.

Java




public class Client {
    public static void main(String[] args) {
        FlyweightFactory factory = new FlyweightFactory();
 
        Flyweight flyweight1 = factory.getFlyweight("shared");
        Flyweight flyweight2 = factory.getFlyweight("shared");
 
        flyweight1.operation();  // Output: ConcreteFlyweight: shared
        flyweight2.operation();  // Output: ConcreteFlyweight: shared
    }
}


In this pattern, the intrinsic state is shared among multiple instances, while the extrinsic state is passed as a parameter to the methods of the flyweight objects. This allows the flyweight objects to be reused in different contexts, reducing memory usage and improving performance.

Flyweight Method Design Pattern in Java

A flyweight design pattern or flyweight method is defined as a structural pattern that is used to minimize memory usage or computational expenses by sharing as much as possible with other similar objects.

The key idea behind the Flyweight pattern is to use shared objects to support large numbers of fine-grained objects efficiently.

Important Topics for Flyweight Design Patterns

  • Key Component of Flyweight Design Patterns:
  • Example of flyweight Design Patterns:
  • Use Case of Flyweight Design Patterns in Java
  • Advantages of Flyweight Design Patterns in Java
  • Disadvantages of Flyweight Design Patterns in Java

Similar Reads

Key Component of Flyweight Design Patterns:

Flyweight Interface/Abstract Class: Define an interface or an abstract class that declares the methods that must be implemented by concrete flyweight objects. This interface usually represents the common functionality shared among the flyweight objects....

Example of flyweight Design Patterns:

...

Use Case of Flyweight Design Patterns in Java

...

Advantages of Flyweight Design Patterns in Java

...

Disadvantages of Flyweight Design Patterns in Java

...