Flyweight Pattern

Category : Design Patterns | Sub Category : Structural design patterns | By Prasad Bonam Last updated: 2023-07-09 09:05:16 Viewed : 324

Flyweight Pattern:

The Flyweight pattern is a structural design pattern that aims to minimize memory usage by sharing common state between multiple similar objects. It is used when you need to create a large number of objects that have similar properties, and the memory usage becomes a concern. The Flyweight pattern achieves this by separating the intrinsic (shared) state and extrinsic (unique) state of the objects. Here is an example of implementing the Flyweight pattern in Java:

import java.util.HashMap; import java.util.Map; // Flyweight interface interface Shape { void draw(); } // Concrete Flyweight class 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("Drawing Circle[color=" + color + ", x=" + x + ", y=" + y + ", radius=" + radius + "]"); } } // Flyweight Factory class ShapeFactory { private static final Map<String, Shape> shapeCache = new HashMap<>(); public static Shape getCircle(String color) { Shape circle = shapeCache.get(color); if (circle == null) { circle = new Circle(color); shapeCache.put(color, circle); } return circle; } } // Client code public class Client { private static final String[] COLORS = { "Red", "Green", "Blue", "Yellow", "Black" }; 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); } }

In this example:

  • The Shape interface represents the flyweight interface and declares the draw() method.
  • The Circle class is a concrete flyweight class that implements the Shape interface. It stores the intrinsic state (color) and can have extrinsic state (position and radius) set by the client.
  • The ShapeFactory class acts as a flyweight factory. It maintains a cache (in the form of a Map) of already created circles, keyed by their color. When the getCircle() method is called with a color, it either returns an existing circle from the cache or creates a new circle if it does not exist.
  • The client code in the Client class demonstrates the usage of the flyweight objects. It repeatedly requests circles with random colors and sets their position and radius. The draw() method is then called on each circle, demonstrating the sharing of the common state (color) among multiple circle objects.

By using the Flyweight pattern, you can reduce memory usage by sharing common state among similar objects, which is especially beneficial when dealing with a large number of objects. The intrinsic state is shared, while the extrinsic state can be varied. This pattern helps improve performance and optimize resource usage in memory-constrained scenarios.

Related Articles

Leave a Comment: