Flyweight Pattern

Flyweight is a structural pattern that is used to reduce the number of objects created and to decrease memory footprint and increase performance. Flyweight tries to reuse already existing similar kind objects by storing them and creates new object when no matching object is found.

The following example demonstrates using Flyweight pattern by drawing 20 circles of different locations but creating only 3 objects. Only 3 colors are available so color property is used to check already existing Circle objects.

Flyweight Pattern UML Diagram

Step 1 : Create an interface Shape

public interface Shape {
void draw();
}

Step 2 : Create concrete class implementing Shape interface

public class Circle implements Shape {
private String color;
private int x;
private int y;
private int radius;

public Circle(String color) {
this.color = color;
}

public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}

public void setRadius(int radius) {
this.radius = radius;
}

@Override
public void draw() {
System.out.println("Circle: Draw() [Color : " + color + ", x : " + x + ", y :" + y + ", radius :" + radius + "]");
}
}

Step 3 : Create a factory to generate object of concrete class based on given information

public class ShapeFactory {
private static final Map<String, Shape> circleMap = new HashMap<>();

public static Shape getCircle(String color) {
Circle circle = (Circle)circleMap.get(color);

if (circle == null) {
circle = new Circle(color);
circleMap.put(color, circle);
System.out.println("Creating circle of color : " + color);
}

return circle;
}
}

Step 4 : Use the factory to get object of concrete class by passing an information such as color

public class FlyweightPatternDemo {
private static final String colors[] = {"Red", "Green", "Blue"};

public static void main(String[] args) {
for (int i=0; i < 10; i++) {
Circle circle = (Circle)ShapeFactory.getCircle(getRandomColor());
circle.setX(getRandomX());
circle.setY(getRandomY());
circle.setRadius(100);
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);
}
}

The output will be :

Creating circle of color : Blue
Circle: Draw() [Color : Blue, x : 50, y :30, radius :100]Circle: Draw() [Color : Blue, x : 89, y :66, radius :100]Creating circle of color : Red
Circle: Draw() [Color : Red, x : 97, y :55, radius :100]Circle: Draw() [Color : Blue, x : 30, y :75, radius :100]Circle: Draw() [Color : Blue, x : 37, y :4, radius :100]Creating circle of color : Green
Circle: Draw() [Color : Green, x : 88, y :20, radius :100]Circle: Draw() [Color : Red, x : 26, y :45, radius :100]Circle: Draw() [Color : Blue, x : 17, y :5, radius :100]Circle: Draw() [Color : Red, x : 98, y :78, radius :100]Circle: Draw() [Color : Blue, x : 96, y :44, radius :100]

Leave a Reply

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