Flyweight Pattern
Introduction:
Flyweight pattern is a structural design pattern which helps to reduce the memory consumptions of a object by sharing some of its data with other similar objects.
The flyweight pattern is useful when dealing with large numbers of objects with simple repeated elements that would use a large amount of memory if individually stored. It is common to hold shared data in external data structures and pass it to the objects temporarily when they are used.
One important feature of flyweight objects is that they are immutable. This means that they cannot be modified once they have been constructed.
The idea of sharing data structures is called hash consing. Hash consing is a technique used to share values that are structurally equal. When a value is constructed, such as a cons cell, the technique checks if such a value has been constructed before, and if so reuses the previous value, avoiding a new memory allocation.
source: geeksforgeeks
Intrinsic and Extrinsic States
Suppose in a text editor when we enter a character, an object of Character class is created, the attributes of the Character class are {name, font, size}. We do not need to create an object every time client enters a character since letter ‘B’ is no different from another ‘B’ . If client again types a ‘B’ we simply return the object which we have already created before. Now all these are intrinsic states (name, font, size), since they can be shared among the different objects as they are similar to each other.
Now we add to more attributes to the Character class, they are row and column. They specify the position of a character in the document. Now these attributes will not be similar even for same characters, since no two characters will have the same position in a document, these states are termed as extrinsic states, and they can’t be shared among objects.
Extrinsic states: Unique attributes of the object
When to use,
Steps to design flyweight pattern.
- Divide the target class's state into: shareable (intrinsic) state, and non-shareable (extrinsic) state.
- Remove the non-shareable state from the class attributes, and add it the calling argument list of affected methods.
- Create a Factory that can cache and reuse existing class instances.
- The client must use the Factory instead of the new operator to request objects.
- The client (or a third party) must look-up or compute the non-shareable state, and supply that state to class methods.
UML Class Diagram:
Example:
You are developing the gaming program where you need to show lot of trees for decorative purpose.
There are two types of the trees 1. Green 2. Brown, and height is 10 for all the trees.
Tree has the below attributes.
2. Height (10)
3. Position (X, Y coordinates).
Color attribute takes "5' bytes to store the value ("Green" or "Brown"). Height takes 4 bytes (int value). X, Y coordinates takes 8 bytes (4 + 4 int value).
One tree object takes 17 bytes. If we construct 100000 trees, it takes 1700 KB. Color and height consumes 900 KB. Here, we are storing same color and height value in all the objects, we are wasting 900KB to store duplicate values. Since Color and height values are common, it can be considered as the intrinsic states and can be shared with other objects. In this we can reduce the size of the 100000 trees as 834 bytes. Position (X, Y ) of the tree can't be same for all the trees. It can't be shareable.
Flyweight patterns helps to reduce the memory consumption of a object by storing the intrinsic state inside the object , and passing the extrinsic states by passing as the argument of the method or storing outside of the Tree object. Here, Tree object is a flyweight object.
Implementation:
Step 1: Identify the intrinsic and extrinsic states of the class
Step 2: Create Flyweight class.
Step 3: Create Flyweight factory method.
Step 4: Context class
Step 5: Client
Step 1: Identify the intrinsic and extrinsic states of the class.
This part has done in the above section. Color and height is same for all the objects (color may be Green or Brown, height is constant for all the trees 10 ). Position of the tree can't be same for all the trees. It can't be shareable.
Extrinsic states : Position (X, Y coordinates).