Java flyweight design pattern comes under structural design patterns and it is used to reduce number of object creation by reusing the existing similar objects. Only create a new object if there is no matching object.
Main advantage of flyweight design pattern is that, it reduces memory consumption by controlling the amount object creation.
Let’s discuss flyweight design pattern by below example. We are going to create 10 Rectangle with different colors but will restrict the object creation. Max 3 objects will be created.
Example
Shape.java
package com.w3schools; public interface Shape { public void createShape(); } |
Rectangle.java
package com.w3schools; public class Rectangle implements Shape { private String color; private int length; private int width; public Rectangle(String color){ this.color = color; } @Override public void createShape() { System.out.println("Rectangle created with following properties:"); System.out.println("Color: " + color); System.out.println("Length: " + length); System.out.println("Width: " + width); } public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } } |
ShapeFactory.java
package com.w3schools; import java.util.HashMap; public class ShapeFactory { private static final HashMap<String, Rectangle> rectangleMap = new HashMap<String, Rectangle>(); public static Shape getCircle(String color) { Rectangle rectangle = (Rectangle)rectangleMap.get(color); if(rectangle == null) { rectangle = new Rectangle(color); rectangleMap.put(color, rectangle); System.out.println("Creating circle of color : " + color); } return rectangle; } } |
FlyweightPatternTest.java
package com.w3schools; public class FlyweightPatternTest { private static final String colors[] = { "Red", "Green", "Blue"}; private static String getRandomColor() { return colors[(int)(Math.random()*colors.length)]; } private static int getRandomLength() { return (int)(Math.random()*100 ); } private static int getRandomWeight() { return (int)(Math.random()*100); } public static void main(String args[]){ for(int i=0; i < 10; ++i) { Rectangle rectangle = (Rectangle)ShapeFactory.getCircle(getRandomColor()); rectangle.setLength(getRandomLength()); rectangle.setWidth(getRandomWeight()); rectangle.createShape(); } } } |
Output
Creating circle of color : Blue Rectangle created with following properties: Color: Blue Length: 0 Width: 43 Creating circle of color : Red Rectangle created with following properties: Color: Red Length: 6 Width: 9 Creating circle of color : Green Rectangle created with following properties: Color: Green Length: 24 Width: 24 Rectangle created with following properties: Color: Green Length: 68 Width: 26 Rectangle created with following properties: Color: Blue Length: 54 Width: 34 Rectangle created with following properties: Color: Green Length: 92 Width: 58 Rectangle created with following properties: Color: Blue Length: 12 Width: 31 Rectangle created with following properties: Color: Blue Length: 56 Width: 0 Rectangle created with following properties: Color: Blue Length: 43 Width: 27 Rectangle created with following properties: Color: Blue Length: 40 Width: 80 |