The objective of this pattern is to use sharing to support a large number of objects that have part of their internal state in common where the other part of the state can vary.
A flyweight is an object that minimizes memory usage by sharing the possible data with other similar objects. It is a way to use objects in large numbers when a simple repeated representation would use an unwanted amount of memory. Often some parts of the object state can be shared, and it is common practice to hold them in external data structures and pass them to the objects temporarily when they are used.
Real Life Examples
Modern web browser
Web browsers use this technique to prevent loading same images twice. When the browser loads a web page, it traverses through all images on that web page and loads all new images from the Internet and places them the internal cache. For already loaded images, a flyweight object is created, which has some unique data like position within the page, but everything else is referenced to the cached one.
In the counter strike, there are a large number of player objects and a player object contains common properties like activity of the player, type of player. There are some additional properties like firing weapons. Creating a large number of player objects is a necessity however it would incur a huge memory cost. Note that although the activity and name of a player are the same, their weapons can vary greatly. The solution is to keep the common state of soldiers in a shared object and inject the additional properties.
How to reuse the object those are already created?
1. Reuse the object that already existing (earlier created and stored)
2. Create the new object when no matching object is found.
Similarities between objects are stored inside of the objects (intrinsic), and differences are moved outside of the objects and placed in the client code (extrinsic).
What problems can the Flyweight design pattern solve?
Large numbers of objects should be supported efficiently.
Creating large numbers of objects should be avoided.
What solution does the Flyweight design pattern describe?
Define Flyweight objects that
Store intrinsic (invariant) state that can be shared and
Provide an interface through which extrinsic (variant) state can be passed in.
Why do we care about the number of objects in our program?
The excess number of objects may lead to errors related to memory like java.lang.OutOfMemoryError less number of objects reduces the memory. Although creating an object in Java is really fast, we can still reduce the execution time of our program by sharing objects.
Advantages of Flyweight pattern
It reduces the number of objects. This can be used to reduce memory requirements and object creational cost. It also reduces database storage devices required if the objects are persisted.
Flyweight Pattern Example in JDK
1. All the valueOf() method of all wrapper classes uses cached objects showing the use of Flyweight design pattern.
2. The best example is Java String class String Pool implementation.
One important feature of flyweight objects is that they are immutable. This means that they cannot be modified once they have been constructed.