What are the practical use differences between Flyweight vs Singleton patterns?
They are unrelated:
Flyweight is reusing (obviously immutable) instances of a class wherever possible, rather than creating new instances of a class with the same "value", which saves CPU and memory.
Singleton is when there is only ever one instance of a (usually mutable) class. It is often used in multi-threaded environments to facilitate all threads using a single instance and "seeing" the same state.
Flyweight is used when there is clear segregation of the object's intrinsic and extrinsic state exists. Intrinsic state can be shared across other extrinsic objects. For example a header of the documents can be classified as intrinsic state of the object and can be shared across. But rest of the data in the document may be very dynamic and filled by user(extrinsic data). To represent intrinsic data a flyweight object can be created and maintained inside flyweight factory. Flyweight factory object will serve as collection of flyweight(header objects) and can be populated earlier and shared across various instances of the document in our case.
A singleton is usually used to represent objects there is only one of, such as a keyboard, the computer screen, main memory. It could represent more abstract things like a messaging dispatch centre or coordinator. A database link for your whole application.
Collection of Objects vs Single Objects
You could think of both a Singleton and a Flyweight as having a factory producing an object. A key difference is that the Singleton factory always returns the same object. A Flyweight factory in contrast is parameterized. The creation function takes an argument. Depending on the value of the argument a different Flyweight object will be returned.
You may have a singleton representing the current page being worked on in a word processor. The individual characters on the page however may be represented as Flyweights.
The similarity with a singleton here is that each occurrence of the character 'A' will refer to the same object. So giving 'A' as a parameter to the Flyweight factory will always return the same object. Hence there is a a singleton like behavior in that every time you create an 'A' object you get the same object returned.
The distinction from a Singleton factory is that the Flyweight factory allows a whole class of related objects to be created.
Mutability
A Singleton is often mutable. However with Flyweights we tend to keep mutable state outside of the Flyweight. So e.g. if we have flyweight objects representing characters in a document, then the character position would typically be stored separate from the Flyweight object.
While I don't rule it out, I cannot think of any case where you would want to make the Flyweight object itself mutable. Imagine you have a Flyweight representing the character 'B' and then you change it to 'A'. Then 'B' in your document would change to 'A'. That would be highly undesirable.
Likewise if Flyweights represent color. It would confusing if you created a red color and it was green instead because its underlying flyweight object was changed to green somewhere else in the code.
Examples
Examples from Cocoa (Objective-C):
UIApplication.sharedApplication.statusBarHidden = YES
This is obviously a single object and you can change its state.
UIColor *red = [UIColor redColor]
In this case it is not obvious if we get a new object each time or not. Because colors are immutable, the maintainers of this API are free to implement common colors such as red, blue, green, yellow and gray as Flyweight objects. That means there is a pool of common objects which get reused when you call the constructor.
To the user it makes no difference whether a flyweight pattern is used behind the scenes or not. This is a reason why I will claim that immutability is a desirable property for flyweight objects.
Also note how parameterization is implicit in this case. You pick color, by using different named constructors. yellowColor
, blueColor
etc.
Flyweight is when you have many different kinds of a single thing.
Singleton is when you have a single thing.
For example, you might use the Flyweight Pattern to represent keyboard characters. One object for a
, one for b
, etc. They are both characters, but different kinds of characters.
In contrast, you might use a singleton to represent the keyboard. There can only be one.