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.
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.
Let’s see the following pros of the flyweight design pattern.
Let’s see the following cons of the flyweight design pattern.
also read:
Let’s see the following scenarios where we have to use the Flyweight pattern.
Let’s see the following UML class diagram for the flyweight design pattern and it illustrates about some important components classes.
It declares an interface through which flyweights can receive.
It implements the Flyweight interface and it must be shareable.
It is not all Flyweight subclasses need to be shared. The Flyweight interface enables sharing, but it doesn’t enforce it.
It creates and manages flyweight objects.
It maintains a reference to flyweight(s).
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.
/** * */ package com.doj.patterns.structural.flyweight; /** * @author Dinesh.Rajput * */ public interface Employee { void work(); }
/** * */ 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); } }
/** * */ 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; } }
/** * */ 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); } }
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
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…