Flyweight Design Pattern in Java

By | July 5, 2020

In this post, We will talk and learn about the FlyweightDesign Pattern in Java.

Key Points About Flyweight Design Pattern :

Flyweight Design Pattern is mainly used to reduce the number of objects created and to decrease memory footprint and increase the performance of the application. This design pattern falls under structural pattern as you can see, this pattern provides ways to decrease objects count thus improving the object structure of the application.

Flyweight Design Pattern works on below principle:

Flyweight patterns help us to reuse already existing similar kinds of objects by storing them and creates new objects only when no matching object is found.

Flyweight Pattern In JDK:

Now Let’s move towards the implementation of the Flyweight Design Pattern.

Below is the complete source code:

Flyweight Design Pattern

Shape.java

 

Circle.java

 

ShapeFactory.java

 

ClientTest.java

Output pf this client Program: 

Circle is created with color : Blue
Circle: Drawn() [Color : Blue, xPoint : 30, yPoint :68, circleRadius :5
Circle is created with color : Red
Circle: Drawn() [Color : Red, xPoint : 2, yPoint :62, circleRadius :5
Circle is created with color : Green
Circle: Drawn() [Color : Green, xPoint : 18, yPoint :97, circleRadius :5
Circle: Drawn() [Color : Green, xPoint : 27, yPoint :68, circleRadius :5
Circle is created with color : White
Circle: Drawn() [Color : White, xPoint : 60, yPoint :93, circleRadius :5
Circle: Drawn() [Color : Green, xPoint : 21, yPoint :49, circleRadius :5
Circle: Drawn() [Color : Red, xPoint : 64, yPoint :62, circleRadius :5
Circle is created with color : Black
Circle: Drawn() [Color : Black, xPoint : 49, yPoint :48, circleRadius :5
Circle: Drawn() [Color : Red, xPoint : 2, yPoint :75, circleRadius :5
Circle: Drawn() [Color : Black, xPoint : 47, yPoint :27, circleRadius :5

You May Also Like:

Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java
Abstract Factory Pattern in Java
Builder Pattern in Java
Adapter Design Pattern in Java
Decorator Design Pattern in Java
Facade Design Pattern in Java
Composite Design Pattern in Java

That’s all about the Flyweight Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *