Design Pattern

Flyweight Pattern – Design Patterns in Java

<p>Flyweight pattern comes under the structural design pattern as like Adapter&comma; Bridge&comma; Decorator&comma; Composition design patterns of the <strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;design-patterns&lowbar;25&sol;">23 GoF Design Patterns<&sol;a><&sol;strong>&period; This design pattern apply to improve the performance of application by reusing the existing similar kind of objects&period; This pattern store the similar kind of objects into cache to reuse&comma; it creates new object when no matching object is found&period;<&sol;p>&NewLine;<h2>The Flyweight Pattern<&sol;h2>&NewLine;<div style&equals;"text-align&colon; left&semi;">&NewLine;<blockquote><p><strong>According to the Gang of Four&colon;<&sol;strong><&sol;p>&NewLine;<div style&equals;"text-align&colon; left&semi;">Use sharing to support large numbers of fine-grained objects efficiently&period;<&sol;div>&NewLine;<&sol;blockquote>&NewLine;<&sol;div>&NewLine;<p>Due to reuse of the number of objects in to application&comma; Flyweight pattern reduce creation of the number of objects and it decreases memory usage and increase performance&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px 16px 16px 16px&semi;">&NewLine;<h2 dir&equals;"ltr">Spring 5 Design Pattern Book<&sol;h2>&NewLine;<div dir&equals;"ltr" style&equals;"color&colon; &num;20124d&semi; font-family&colon; 'calibri' &comma; sans-serif&semi; font-size&colon; 13pt&semi; text-align&colon; justify&semi;">You could purchase my <strong>Spring 5 book<&sol;strong> that is with title name &&num;8220&semi;<strong>Spring 5 Design Pattern<&sol;strong>&&num;8220&semi;&period; This book is available on the <a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;ie&equals;UTF8&amp&semi;qid&equals;1507925340&amp&semi;sr&equals;8-1&amp&semi;keywords&equals;spring&plus;5&plus;design&plus;pattern" target&equals;"&lowbar;blank" rel&equals;"noopener"><strong>Amazon<&sol;strong><&sol;a> and <a href&equals;"https&colon;&sol;&sol;www&period;packtpub&period;com&sol;application-development&sol;spring-5-design-patterns" target&equals;"&lowbar;blank" rel&equals;"noopener"><strong>Packt<&sol;strong><&sol;a> publisher website&period; Learn various <strong>design patterns<&sol;strong> and <strong>best practices<&sol;strong> in Spring 5 and use them to solve common design problems&period; You could use author discount to purchase this book by using code- &&num;8220&semi;<strong>AUTHDIS40<&sol;strong>&&num;8220&semi;&period;<&sol;div>&NewLine;<div dir&equals;"ltr"><&sol;div>&NewLine;<div dir&equals;"ltr"><a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;s&equals;books&amp&semi;ie&equals;UTF8&amp&semi;qid&equals;1512607966&amp&semi;sr&equals;1-1&amp&semi;keywords&equals;dinesh&plus;rajput" target&equals;"&lowbar;blank" rel&equals;"noopener"><img class&equals;"aligncenter wp-image-3053 size-medium" title&equals;"Spring-5-Design-Pattern" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;03&sol;Spring-5-design-pattern-243x300&period;jpg" width&equals;"243" height&equals;"300" &sol;><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;<h2><strong>Pros of the Flyweight Pattern<&sol;strong><&sol;h2>&NewLine;<p>Let&&num;8217&semi;s see the following pros of the flyweight design pattern&period;<&sol;p>&NewLine;<ul>&NewLine;<li>Flyweight pattern improves the performance of application by reducing the number of objects&period;<&sol;li>&NewLine;<li>This pattern reduces the amount of memory and storage devices required if the objects are persisted&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2><strong>Cons of the Flyweight Pattern<&sol;strong><&sol;h2>&NewLine;<p>Let&&num;8217&semi;s see the following cons of the flyweight design pattern&period;<&sol;p>&NewLine;<ul>&NewLine;<li>If your software does not have memory concerns flyweight design can lead to complicating the code with no performance gain&period;<&sol;li>&NewLine;<li>It makes code complicated&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<div style&equals;"text-align&colon; left&semi;">&NewLine;<blockquote><p><strong>also read&colon;<&sol;strong><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;adapter-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;adapter-design-pattern&sol;">Adapter Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;bridge-design-patterns&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;bridge-design-patterns&sol;">Bridge Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;decorator-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;decorator-design-pattern&sol;">Decorator Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;facade-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;facade-design-pattern&sol;">Facade Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;proxy-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;proxy-pattern-design-patterns-java&sol;">Proxy Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;builder-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;builder-design-pattern&sol;">Builder Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;creational-design-patterns&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;creational-design-patterns&sol;&sol;">Creational GoF Design Patterns<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;behavioral-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;behavioral-design-pattern&sol;">Behavioral GoF Design Patterns<&sol;a><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;blockquote>&NewLine;<&sol;div>&NewLine;<h2><strong>Applicability<&sol;strong><&sol;h2>&NewLine;<p>Let&&num;8217&semi;s see the following scenarios where we have to use the Flyweight pattern&period;<&sol;p>&NewLine;<ul>&NewLine;<li>When an application similar kind of objects in very large amount&period;<&sol;li>&NewLine;<li>When you to reduce the storage cost of application&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2><strong>UML class diagram for Flyweight Design Pattern<&sol;strong><&sol;h2>&NewLine;<p>Let&&num;8217&semi;s see the following UML class diagram for the flyweight design pattern and it illustrates about some important components classes&period;<&sol;p>&NewLine;<p><img class&equals;"aligncenter wp-image-3580 size-full" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;12&sol;flyweight&period;gif" alt&equals;"Flyweight Pattern Design Patterns in Java" width&equals;"467" height&equals;"289" &sol;><&sol;p>&NewLine;<h3>Flyweight<&sol;h3>&NewLine;<p>It declares an interface through which flyweights can receive&period;<&sol;p>&NewLine;<h3>ConcreteFlyweight<&sol;h3>&NewLine;<p>It implements the Flyweight interface and it must be shareable&period;<&sol;p>&NewLine;<h3>UnsharedConcreteFlyweight<&sol;h3>&NewLine;<p>It is not all Flyweight subclasses need to be shared&period; The Flyweight interface enables sharing&comma; but it doesn&&num;8217&semi;t enforce it&period;<&sol;p>&NewLine;<h3>FlyweightFactory<&sol;h3>&NewLine;<p>It creates and manages flyweight objects&period;<&sol;p>&NewLine;<h3>Client<&sol;h3>&NewLine;<p>It maintains a reference to flyweight&lpar;s&rpar;&period;<&sol;p>&NewLine;<h2><strong>Sample Implementation for Flyweight Design Pattern<&sol;strong><&sol;h2>&NewLine;<p>We are going to create a Employee interface and concrete class Manager implementing the Employee interface&period; A factory class EmployeeFactory is defined as a next step&period;<&sol;p>&NewLine;<h3>Step 1&colon; Create an interface&period;<br &sol;>&NewLine;Employee&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;structural&period;flyweight&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public interface Employee &lbrace; &NewLine;&Tab; &NewLine;&Tab;void work&lpar;&rpar;&semi; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 2&colon; Create concrete Manager class implementing the Employee interface&period;<br &sol;>&NewLine;Manager&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;structural&period;flyweight&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class Manager implements Employee &lbrace; &NewLine;&Tab; &NewLine;&Tab;private String department&semi; &NewLine;&Tab;private double salary&semi; &NewLine;&Tab; &NewLine;&Tab;&Tab;public Manager&lpar;String department&rpar; &lbrace; &NewLine;&Tab;&Tab;super&lpar;&rpar;&semi; &NewLine;&Tab;&Tab;this&period;department &equals; department&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine; &NewLine;&Tab;public String getDepartment&lpar;&rpar; &lbrace; &NewLine;&Tab;&Tab;return department&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine; &NewLine;&Tab;public void setDepartment&lpar;String department&rpar; &lbrace; &NewLine;&Tab;&Tab;this&period;department &equals; department&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine; &NewLine;&Tab;public double getSalary&lpar;&rpar; &lbrace; &NewLine;&Tab;&Tab;return salary&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine; &NewLine;&Tab;public void setSalary&lpar;double salary&rpar; &lbrace; &NewLine;&Tab;&Tab;this&period;salary &equals; salary&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine; &NewLine;&Tab;&commat;Override &NewLine;&Tab;public void work&lpar;&rpar; &lbrace; &NewLine;&Tab;&Tab;System&period;out&period;println&lpar;"Manager of Department&colon; "&plus;department&plus;" is taking salary&colon; "&plus;salary&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 3&colon; Create a factory EmployeeFactory to generate object of concrete class Manager based on given information&period;<br &sol;>&NewLine;EmployeeFactory&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;structural&period;flyweight&semi; &NewLine; &NewLine;import java&period;util&period;HashMap&semi; &NewLine;import java&period;util&period;Map&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class EmployeeFactory &lbrace; &NewLine;&Tab;private static final Map&lt&semi;String&comma; Employee&gt&semi; managerMap &equals; new HashMap&lt&semi;&gt&semi;&lpar;&rpar;&semi; &NewLine; &NewLine;&Tab;public static Employee getManager&lpar;String department&rpar; &lbrace; &NewLine;&Tab;&Tab;Manager manager &equals; &lpar;Manager&rpar;managerMap&period;get&lpar;department&rpar;&semi; &NewLine;&Tab;&Tab; &NewLine;&Tab;&Tab;if&lpar;manager &equals;&equals; null&rpar; &lbrace; &NewLine;&Tab;&Tab;&Tab;manager &equals; new Manager&lpar;department&rpar;&semi; &NewLine;&Tab;&Tab;&Tab;managerMap&period;put&lpar;department&comma; manager&rpar;&semi; &NewLine;&Tab;&Tab;&Tab;System&period;out&period;println&lpar;"Creating manager of department &colon; " &plus; department&rpar;&semi; &NewLine;&Tab;&Tab;&rcub; &NewLine;&Tab;&Tab;return manager&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 4&colon; Use the factory to get object of concrete class by passing an information such as department&period;<br &sol;>&NewLine;FlyweightPatternDemo&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;structural&period;flyweight&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class FlyweightPatternDemo &lbrace; &NewLine;&Tab; &NewLine;&Tab; private static final String departments&lbrack;&rsqb; &equals; &lbrace; "IT"&comma; "ME"&comma; "EE"&comma; "CO"&comma; "PWD"&comma; "NR"&comma; "ST"&comma; "IAS"&rcub;&semi; &NewLine;&Tab; &NewLine;&Tab;&sol;&ast;&ast; &NewLine;&Tab; &ast; &commat;param args &NewLine;&Tab; &ast;&sol; &NewLine;&Tab;public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine;&Tab;&Tab;for&lpar;int i&equals;0&semi; i &lt&semi; 8&semi; &plus;&plus;i&rpar; &lbrace; &NewLine;&Tab;&Tab;&Tab;Manager manager &equals; &lpar;Manager&rpar;EmployeeFactory&period;getManager&lpar;getRandomDepartment&lpar;&rpar;&rpar;&semi; &NewLine;&Tab;&Tab;&Tab;manager&period;setSalary&lpar;getRandomSalary&lpar;&rpar;&rpar;&semi; &NewLine;&Tab;&Tab;&Tab;manager&period;work&lpar;&rpar;&semi; &NewLine;&Tab;&Tab;&rcub; &NewLine;&Tab;&rcub; &NewLine;&Tab;private static String getRandomDepartment&lpar;&rpar; &lbrace; &NewLine;&Tab;&Tab;return departments&lbrack;&lpar;int&rpar;&lpar;Math&period;random&lpar;&rpar;&ast;departments&period;length&rpar;&rsqb;&semi; &NewLine;&Tab;&rcub; &NewLine;&Tab; &NewLine;&Tab;private static double getRandomSalary&lpar;&rpar; &lbrace; &NewLine;&Tab;&Tab;return &lpar;double&rpar;&lpar;Math&period;random&lpar;&rpar;&ast;100000&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 5&colon; Let&&num;8217&semi;s run this demo class and verify the output&period;<&sol;h3>&NewLine;<pre class&equals;"highlight">Creating manager of department &colon; PWD &NewLine;Manager of Department&colon; PWD is taking salary&colon; 37743&period;55396460834 &NewLine;Creating manager of department &colon; IAS &NewLine;Manager of Department&colon; IAS is taking salary&colon; 45169&period;53270870482 &NewLine;Creating manager of department &colon; IT &NewLine;Manager of Department&colon; IT is taking salary&colon; 44540&period;66001403507 &NewLine;Creating manager of department &colon; ME &NewLine;Manager of Department&colon; ME is taking salary&colon; 79617&period;26799730789 &NewLine;Creating manager of department &colon; NR &NewLine;Manager of Department&colon; NR is taking salary&colon; 48541&period;33901789731 &NewLine;Manager of Department&colon; IT is taking salary&colon; 45679&period;588691403296 &NewLine;Creating manager of department &colon; EE &NewLine;Manager of Department&colon; EE is taking salary&colon; 20382&period;416449759898 &NewLine;Manager of Department&colon; IT is taking salary&colon; 27847&period;203390300823 &NewLine; &NewLine;<&sol;pre>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;composite-pattern-design-patterns-java&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;proxy-pattern-design-patterns-java&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '3579'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

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…

4 years ago

Decorator Pattern using Lambda

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

4 years ago

Delegating pattern using lambda

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

4 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;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

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

4 years ago

Scheduler @Scheduled Annotation Spring Boot

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

4 years ago