Design Pattern

Flyweight Pattern – Design Patterns in Java

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

You could purchase my Spring 5 book that is with title name “Spring 5 Design Pattern“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.

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.

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

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago