Using Flyweight Design Pattern In Java
In this article, explore the Flyweight design pattern in Java.
Join the DZone community and get the full member experience.Join For Free
I am here to discuss another structural design pattern named Flyweight Design Pattern.
Flyweight Design Pattern
- The Flyweight Design Pattern is a Structural Design Pattern and one of the Gang of Four design patterns.
- The Flyweight pattern is used to minimize the memory usage by sharing as much data as possible with other similar objects.
- The Flyweight pattern provides a way to reduce the number of objects created and to decrease memory footprint and increase performance.
- The Flyweight pattern tries to reuse already existing similar kind objects by storing them in a collection which act like a cache and creates new object when no matching object is found.
- The Flyweight objects we create as immutable. This means that they cannot be modified once they have been constructed. Making flyweight objects as immutable helps while sharing them with other objects.
- The data structures for graphical representation of characters in a Word Processor is a very good classical example of Flyweight pattern.
- The String Interning is another very good example of Flyweight pattern.
- The Flyweight pattern helps us avoiding having large number of objects and allow us to effectively used the created objects by reusing them as much as possible.
- The Flyweight object essentially has two different kind of attributes –
- Intrinsic - An intrinsic (invariant) state attribute is stored and shared in the flyweight object. It is independent of flyweight’s context. So, as the best practice we should make intrinsic states immutable.
- Extrinsic - An extrinsic (variant) state attribute does not store and share in the flyweight object because it depends on flyweight’s context and varies as context change. Generally, we store and maintain the extrinsic state in the Client objects. We need to pass this extrinsic state to the flyweight object for object creation and processing.
Coffee Shop Example Without Using Flyweight Design Pattern
Suppose there is a Coffee shop that serves coffee with different flavors and latte art.
Here is the code for CoffeeFlavour enum to provide supported flavors of coffee.
Also here the code for CoffeeLatteArt enum to provide supported latte art of coffee. Latte art is a method of preparing coffee created by pouring microfoam into a shot of espresso and resulting in a pattern or design on the surface of the latte. It can also be created or embellished by simply "drawing" in the top layer of foam.
Below is the code for Coffee class. I made Coffee as immutable object.
Also, to make the example comparable with another example by using Flyweight pattern, I wrote a factory named CoffeeFactory which will:
- Allow using restricted access to the Coffee constructor (protected).
- Provide a factory method named makeCoffee to create objects of Coffee class.
- Create and maintain a collection of total number of Coffee objects created.
Here's the code for Order class:
Now the code for CoffeeShop class. CoffeeShop maintains the list of orders served.
And now the Main class to execute and test the output.
And below is the output of the the program:
Please notice that the number of Coffee Objects are 10.
Coffee Shop Example by Using Flyweight Design Pattern
Now the same example by using Flyweight Design Pattern. I will try to highlight the differences and also the benefits of using flyweight pattern.
Here's the code for the Coffee class:
Hare's the code for CoffeeFactory class:
As you can see that the CoffeeFactory maintains a collection using CoffeeFlavour (intrinsic attribute) as key. This will help us reducing the number of objects created and reusing the existing one.
Here's the code for the Order class:
Here's the code for CoffeeShop class:
Now its time to write Main class to execute and test the output:
And below is the output of the program:
Same output, but the number of Coffee objects is only 5 by using the Flyweight pattern.
That's all! I hope this tutorial helps you understand the Flyweight Design Pattern.
Source code can be found here: Flyweight Design Pattern Sample Code
Liked the article? Please don't forget to press that like button. Happy coding!
Need more articles, please visit my profile: Brijesh Saxena.
Opinions expressed by DZone contributors are their own.