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:
java.lang.Integer#valueOf(int)
(also onBoolean
,Byte
,Character
,Short
,Long
andBigDecimal
)
Now Let’s move towards the implementation of the Flyweight Design Pattern.
Below is the complete source code:
Shape.java
1 2 3 4 5 6 7 8 |
package com.kkjavatutorials.shape; /** * @author KK JavaTutorials * Contract to draw any shape */ public interface Shape { public abstract void draw(); } |
Circle.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.kkjavatutorials.shape.impl; import com.kkjavatutorials.shape.Shape; /** * @author KK JavaTutorials *Implementation to draw Circle */ public class Circle implements Shape { private String color; private int xPoint; private int yPoint; private int circleRadius; public Circle(String color) { this.color = color; } public void setColor(String color) { this.color = color; } public void setxPoint(int xPoint) { this.xPoint = xPoint; } public void setyPoint(int yPoint) { this.yPoint = yPoint; } public void setCircleRadius(int circleRadius) { this.circleRadius = circleRadius; } @Override public void draw() { System.out.println("Circle: Drawn() [Color : " + color + ", xPoint : " + xPoint + ", yPoint :" + yPoint + ", circleRadius :" + circleRadius); } } |
ShapeFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.kkjavatutorials.shape.factory; import java.util.HashMap; import com.kkjavatutorials.shape.Shape; import com.kkjavatutorials.shape.impl.Circle; /** * @author KK JavaTutorials *This class using Flyweight pattern to *reuse already existing similar kind of objects by *storing them and creates new object only when no *matching object is found. */ public class ShapeFactory { //Storing Object in circleMapCache when first time it's created private static final HashMap<String,Circle> circleMapCache = new HashMap<String,Circle>(); public static Shape getCircle(String color) { /* First look up into circleMapCache, If object not found in circleMapCache then only create object*/ Circle circle = circleMapCache.get(color); /*Object not found in circleMapCache so create new Object and then put in circleMapCache so that next time when client ask for similar object then look up into circleMapCache and return*/ if (circle == null) { circle = new Circle(color); circleMapCache.put(color, circle); System.out.println("Circle is created with color : " + color); } return circle; } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.kkjavatutorials.client; import com.kkjavatutorials.shape.factory.ShapeFactory; import com.kkjavatutorials.shape.impl.Circle; /** * @author KK JavaTutorials *Client program makes call to *ShapeFactory.getCircle(getRandomColor()) *then internally it uses Flyweight pattern */ public class ClientTest { private static final String colors[] = {"Green","Red" ,"Black","Blue", "White" }; public static void main(String args[]) { for (int i = 0; i < 10; ++i) { Circle circle = (Circle) ShapeFactory.getCircle(getRandomColor()); circle.setxPoint(getRandomX()); circle.setyPoint(getRandomY()); circle.setCircleRadius(5); circle.draw(); } } private static String getRandomColor() { return colors[(int) (Math.random() * colors.length)]; } private static int getRandomX() { return (int) (Math.random() * 100); } private static int getRandomY() { return (int) (Math.random() * 100); } } |
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.