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?
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.
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.
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.
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.
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'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With