Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the Flyweight pattern

Intent:

The intent of this pattern is to use sharing to support a large number of objects that have part of their internal state in common where the other part of state can vary.

Objects can share state via static fields.

What is the difference between sharing internal state of a large number of objects using the flyweight pattern and using static fields?

Is the pool of objects that the flyweight provides via its Factory what the flyweight is really all about?

like image 899
Sam Leach Avatar asked May 15 '14 16:05

Sam Leach


People also ask

How does a flyweight pattern work?

Flyweight pattern is primarily used to reduce the number of objects created and to decrease memory footprint and increase performance. This type of design pattern comes under structural pattern as this pattern provides ways to decrease object count thus improving the object structure of application.

When would you use the flyweight pattern?

Flyweight pattern is used when we need to create a large number of similar objects (say 105). One important feature of flyweight objects is that they are immutable. This means that they cannot be modified once they have been constructed.

Why is flyweight pattern called flyweight?

The flyweight pattern is used to minimize the amount of memory used when you need to create a large number of similar objects. It accomplishes this by sharing instances. The name derives from the weight classification as you mention but refers to the little amount of memory. That is, memory = weight.

What is the main advantages of flyweight design pattern?

Advantages of Flyweight Design Pattern The Flyweight Pattern contributes to improving the performance of the application by reducing the number of objects. The Flyweight Pattern reduces the memory footprint and saving RAM as the common properties are shared between objects using Intrinsic properties.


1 Answers

Using static fields, there can only ever be one instance of the object in use at any one point in time. Using the flyweight pattern, you can have any number of different instances in use simultaneously, (each one of which is used multiple times). The canonical example of the flyweight pattern is for a text editor, where you need an object instantiated for each and every character in the document. Instead of having one object in memory for each character in a 10,000 word document, you then only need 26 objects, (assuming document only uses lower case letters), one for the letter 'a', one for the letter 'b', etc., and they are reused, transiently, over and over again throughout the document, each time you need to perform some function or action requiring an 'a' object.

EDIT: to answer question from first comment below:
So, since you need 26 different objects, making a Letter class that was a static, or a singleton, wouldn't work. If it was a static, you can't make any instances, and so whatever the static values would have to be appropriate for every place in the code where you made use of it. If it was a singleton, then of course you only have one object. Every property would have to be adjustable (and adjusted) every time you used it. To use this pattern for the letters in the alphabet, you have to have 26 different classes, one for each letter...

Also, the "part of the class that can vary" really means that some fields represent state that is different for every instance of the class. Whereas the part that is in common means that the values of those common fields are in common for all uses of the object matching those state values (all the 'a's for example), not for every instance of the class.

Again, using the text editor as an example. Every place in your code that you need to deal with a character that is an 'a', you would first, go to the data structure that stores your 26 instances of character objects, and fetch the 'a' instance, You would first modify/change the varying properties (the properties not tied to it's nature as an 'a', but perhaps to it's font size, position, color, etc.) to match the needs for this specific character 'a' in the document.
Then you would utilize the object to do whatever it is you need to do with it, and then return it to the storage structure for reuse the next time your code needs an 'a'.

like image 98
Charles Bretana Avatar answered Oct 01 '22 12:10

Charles Bretana