Flyweight pattern comes under the structural design pattern as like Adapter, Bridge, Decorator, Composition design patterns of the 23 GoF Design Patterns. This design pattern apply to improve the performance of application by reusing the existing similar kind of objects. This pattern store the similar kind of objects into cache to reuse, it creates new object when no matching object is found.
The Flyweight Pattern
According to the Gang of Four:
Use sharing to support large numbers of fine-grained objects efficiently.
Due to reuse of the number of objects in to application, Flyweight pattern reduce creation of the number of objects and it decreases memory usage and increase performance.
Spring 5 Design Pattern Book
Pros of the Flyweight Pattern
Let’s see the following pros of the flyweight design pattern.
- Flyweight pattern improves the performance of application by reducing the number of objects.
- This pattern reduces the amount of memory and storage devices required if the objects are persisted.
Cons of the Flyweight Pattern
Let’s see the following cons of the flyweight design pattern.
- If your software does not have memory concerns flyweight design can lead to complicating the code with no performance gain.
- It makes code complicated.
also read:
Applicability
Let’s see the following scenarios where we have to use the Flyweight pattern.
- When an application similar kind of objects in very large amount.
- When you to reduce the storage cost of application.
UML class diagram for Flyweight Design Pattern
Let’s see the following UML class diagram for the flyweight design pattern and it illustrates about some important components classes.
Flyweight
It declares an interface through which flyweights can receive.
ConcreteFlyweight
It implements the Flyweight interface and it must be shareable.
UnsharedConcreteFlyweight
It is not all Flyweight subclasses need to be shared. The Flyweight interface enables sharing, but it doesn’t enforce it.
FlyweightFactory
It creates and manages flyweight objects.
Client
It maintains a reference to flyweight(s).
Sample Implementation for Flyweight Design Pattern
We are going to create a Employee interface and concrete class Manager implementing the Employee interface. A factory class EmployeeFactory is defined as a next step.
Step 1: Create an interface.
Employee.java
/** * */ package com.doj.patterns.structural.flyweight; /** * @author Dinesh.Rajput * */ public interface Employee { void work(); }
Step 2: Create concrete Manager class implementing the Employee interface.
Manager.java
/** * */ package com.doj.patterns.structural.flyweight; /** * @author Dinesh.Rajput * */ public class Manager implements Employee { private String department; private double salary; public Manager(String department) { super(); this.department = department; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public void work() { System.out.println("Manager of Department: "+department+" is taking salary: "+salary); } }
Step 3: Create a factory EmployeeFactory to generate object of concrete class Manager based on given information.
EmployeeFactory.java
/** * */ package com.doj.patterns.structural.flyweight; import java.util.HashMap; import java.util.Map; /** * @author Dinesh.Rajput * */ public class EmployeeFactory { private static final Map<String, Employee> managerMap = new HashMap<>(); public static Employee getManager(String department) { Manager manager = (Manager)managerMap.get(department); if(manager == null) { manager = new Manager(department); managerMap.put(department, manager); System.out.println("Creating manager of department : " + department); } return manager; } }
Step 4: Use the factory to get object of concrete class by passing an information such as department.
FlyweightPatternDemo.java
/** * */ package com.doj.patterns.structural.flyweight; /** * @author Dinesh.Rajput * */ public class FlyweightPatternDemo { private static final String departments[] = { "IT", "ME", "EE", "CO", "PWD", "NR", "ST", "IAS"}; /** * @param args */ public static void main(String[] args) { for(int i=0; i < 8; ++i) { Manager manager = (Manager)EmployeeFactory.getManager(getRandomDepartment()); manager.setSalary(getRandomSalary()); manager.work(); } } private static String getRandomDepartment() { return departments[(int)(Math.random()*departments.length)]; } private static double getRandomSalary() { return (double)(Math.random()*100000); } }
Step 5: Let’s run this demo class and verify the output.
Creating manager of department : PWD Manager of Department: PWD is taking salary: 37743.55396460834 Creating manager of department : IAS Manager of Department: IAS is taking salary: 45169.53270870482 Creating manager of department : IT Manager of Department: IT is taking salary: 44540.66001403507 Creating manager of department : ME Manager of Department: ME is taking salary: 79617.26799730789 Creating manager of department : NR Manager of Department: NR is taking salary: 48541.33901789731 Manager of Department: IT is taking salary: 45679.588691403296 Creating manager of department : EE Manager of Department: EE is taking salary: 20382.416449759898 Manager of Department: IT is taking salary: 27847.203390300823
Could you please fix the indentation for step 2 I mean the manager constructor? Thanks.