Flyweight Pattern in Java – Example Tutorial
Use sharing to support large numbers of fine-grained objects efficiently Flyweight design pattern is a Structural design pattern like Facade pattern , Adapter Pattern and Decorator pattern . Flyweight design pattern is used when we need to create a lot of Objects of a class. Since every object consumes memory space that can be crucial for low memory devices, such as mobile devices or embedded systems, flyweight design pattern can be applied to reduce the load on memory by sharing objects. Before we apply flyweight design pattern, we need to consider following factors:
Flyweight design pattern is used when we need to create a lot of fine grained Objects of a class, flyweight design pattern can be applied to reduce the load on memory by sharing objects.
To apply flyweight pattern, we need to divide Object property into intrinsic and extrinsic properties. Intrinsic properties make the Object unique whereas extrinsic properties are set by client code and used to perform different operations.
For applying flyweight pattern, we need to create a Flyweight factory that returns the shared objects.
Use sharing to support large numbers of fine-grained objects efficiently Flyweight design pattern is a Structural design pattern like Facade pattern , Adapter Pattern and Decorator pattern . Flyweight design pattern is used when we need to create a lot of Objects of a class. Since every object consumes memory space that can be crucial for low memory devices, such as mobile devices or embedded systems, flyweight design pattern can be applied to reduce the load on memory by sharing objects. Before we apply flyweight design pattern, we need to consider following factors:
Flyweight design pattern is used when we need to create a lot of fine grained Objects of a class, flyweight design pattern can be applied to reduce the load on memory by sharing objects.
To apply flyweight pattern, we need to divide Object property into intrinsic and extrinsic properties. Intrinsic properties make the Object unique whereas extrinsic properties are set by client code and used to perform different operations.
For applying flyweight pattern, we need to create a Flyweight factory that returns the shared objects.
Flyweight Factory
The flyweight factory will be used by client programs to instantiate the Object, so we need to keep a map of Objects in the factory that should not be accessible by client application. Whenever client program makes a call to get an instance of Object, it should be returned from the HashMap, if not found then create a new Object and put in the Map and then return it. We need to make sure that all the intrinsic properties are considered while creating the Object.
Flyweight Pattern Example in JDK
All the wrapper classes
valueOf()
method uses cached objects showing use of Flyweight design pattern. The best example is Java String class String Pool implementation.
Flyweight pattern implementation is not useful when the number of intrinsic properties of Object is huge, making implementation of Factory class complex.
Read full article from Flyweight Pattern in Java – Example Tutorial