Skip to main content

Structural Patterns

Flyweight Pattern

A flyweight design pattern can be used to save memory. A flyweight is a structural design pattern instead of creating a large number of similar objects.We can reduce the number of objects that are created by reusing the objects and saving memory. Memory is a huge concern especially when it comes to mobile applications with limited memory. Let's say we are working on a paint app that allows users to draw different shapes that is circles and rectangles. Here we have a shape interface with a draw method on it.

When we have a circle class and the rectangle classes that implements that interface using a circle. The user can draw a circle using a rectangle of course he can try her rectangle to do that. He will have to create a circle and assign a radius for each circle he wants to create. Similarly for each rectangle he will how to set the length and the breadth by creating a separate object by using the flyweight pattern.

You will do all that by creating one single circle and one single rectangle he will see what the problem is he will implement the problem first and then you will resolve that problem of multiple object creation by using the fly design pattern in the next few lectures.

package com.sudhir.patterns.flyweight;

import java.util.HashMap;
import java.util.Map;

public class ShapeFactory {

private static Map<String, Shape> shapes = new HashMap<>();

public static Shape getShape(String type) {
Shape shape = null;
if (shapes.get(type) != null) {
shape = shapes.get(type);
} else {
if (type.equals("circle")) {
shape = new Circle();
} else if (type.equals("rectangle")) {
shape = new Rectangle();
}
shapes.put(type, shape);
}
return shape;

}

}
-----
package com.sudhir.patterns.flyweight;

public abstract class Shape {
public void draw(int radius,String fillColor,String lineColor) {
//no - op
}

public void draw(int length,int breadth,String fillStyle) {
//no - op
}
}

-----

package com.sudhir.patterns.flyweight;

public class Rectangle extends Shape {
private String label;

public Rectangle() {
label = "Rectangle";
}

public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
}

@Override
public void draw(int length, int breadth, String fillStyle) {
System.out.println(
"Drawing a " + label + " with lenght " + length + " Breadth " + breadth + " Fill Style " + fillStyle);
}

}
-----

package com.sudhir.patterns.flyweight;

public class PaintApp {

public void render(int numberOfShapes) {

Shape shape = null;

for (int i = 1; i <= numberOfShapes; i++) {
if (i % 2 == 0) {
shape = ShapeFactory.getShape("circle");
shape.draw(i, "red", "white");
} else {
shape = ShapeFactory.getShape("rectangle");
shape.draw(i, i + i, "dotted");
}
}

}

}

-----

package com.sudhir.patterns.flyweight;

public class Circle extends Shape {

private String label;

public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
}

public Circle() {
label = "Cicle";
}

@Override
public void draw(int radius, String fillColor, String lineColor) {
System.out.println("Drawing a " + label + " with radius " + radius + " Fill color " + fillColor
+ " Line Color " + lineColor);
}

}
------

package com.sudhir.patterns.flyweight;

public class Test {

public static void main(String[] args) {

PaintApp app = new PaintApp();
app.render(10);

}

}

Facade Pattern