Problem being faced

A common problem with heavy weight objects is that they consume so much resources if you keep on creating them without proper strategy. In order to fix the wastage of resources, you definitely may want to avoid the creation of duplicate objects specially for objects which can affect the performance of your application.

The solution

The Flyweight design pattern is meant to be used so that the weight of objects can be reduced on the fly. The basic idea is to reuse the object states.

Flyweight in JDK

A classic example of flyweight design pattern is String class in Java. There is a special area in the memory where String literals are stored and reused as and when required. This area is known as String literal pool. Any new String object created using literal is first compared with those in the pool to determine if there is a match.

If a match is found, the new reference will start pointing to the object in the pool. This means that we just avoided the creation of duplicate object. You can see the flyweight design pattern in action in the below code:

 String str1 = "abc"; String str2 = "abc"; System.out.println(str1==str2);
 

Above code snippet will print true on the console because both str1 and str2 are pointing to the same object in the memory. The object is created in the first line and is then reused in the second line. You may say that Strings are not heavy objects and don’t consume a lot of memory but since they are immutable in Java, their number can easily grow in an application.

How to use flyweight

We can also create similar behavior for custom objects like characters in a word processor program. There is a sense of intrinsic and extrinsic state with objects which is also termed as shareable and non-shareable state of objects. The shareable state should be used as the criteria to detect whether to reuse objects are not. The non-shareable state should be used as meta data and shouldn’t affect the underlying object.

For example color of a character can be controlled dynamically by attaching corresponding meta data while displaying it, thus the same character object can be displayed using multiple colors. Here the character content like “a,b ,c” becomes the intrinsic property and color is the extrinsic property.

Bonus Tip: Flyweight design pattern is also known as Hash Consing in functional programming.

Reference

1) http://en.wikipedia.org/wiki/Flyweight_pattern
2) http://sourcemaking.com/design_patterns/flyweight
3) http://www.codeproject.com/Articles/186002/Flyweight-Design-Pattern

Related Posts

Flyweight Design Pattern admin Design Patterns
Problem being faced A common problem with heavy weight objects is that they consume so much resources if you keep on creating them without proper strategy. In order to fix the wastage of resources, you definitely may want to avoid the creation of duplicate objects specially for objects which can...
<p> <h2>Problem being faced</h2> A common problem with heavy weight objects is that they consume so much resources if you keep on creating them without proper strategy. In order to fix the wastage of resources, you definitely may want to avoid the creation of duplicate objects specially for objects which can affect the <a href="http://www.javaexperience.com/performance-management-for-web-applications/">performance of your application</a>. </p> <h2>The solution</h2> <p> The Flyweight design pattern is meant to be used so that the weight of objects can be reduced on the fly. The basic idea is to reuse the object states. </p> <h2>Flyweight in JDK</h2> <p> <strong>A classic example of flyweight design pattern is String class in Java.</strong> There is a special area in the memory where String literals are stored and reused as and when required. This area is known as String literal pool. Any new String object created using literal is first compared with those in the pool to determine if there is a match. If a match is found, the new reference will start pointing to the object in the pool. This means that we just avoided the creation of duplicate object. You can see the flyweight design pattern in action in the below code: </p> 1 <p>Above code snippet will print true on the console because both str1 and str2 are pointing to the same object in the memory. The object is created in the first line and is then reused in the second line. You may say that Strings are not heavy objects and don't consume a lot of memory but since they are immutable in Java, their number can easily grow in an application.</p> <h2>How to use flyweight</h2> <p>We can also create similar behavior for custom objects like characters in a word processor program. There is a sense of intrinsic and extrinsic state with objects which is also termed as shareable and non-shareable state of objects. T<strong>he shareable state should be used as the criteria to detect whether to reuse objects are not.</strong> The non-shareable state should be used as meta data and shouldn't affect the underlying object. </p> <p>For example color of a character can be controlled dynamically by attaching corresponding meta data while displaying it, thus the same character object can be displayed using multiple colors. Here the character content like "a,b ,c" becomes the intrinsic property and color is the extrinsic property. <strong>Bonus Tip:</strong> Flyweight design pattern is also known as Hash Consing in functional programming. </p> <h2>Reference</h2> <p> 1) http://en.wikipedia.org/wiki/Flyweight_pattern 2) http://sourcemaking.com/design_patterns/flyweight 3) http://www.codeproject.com/Articles/186002/Flyweight-Design-Pattern </p>
Decorator Design Pattern
An Introduction to design patterns
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Latest posts by admin (see all)

Comments

comments