What are the practical use differences between Flyweight vs Singleton patterns?

后端 未结 7 460
攒了一身酷
攒了一身酷 2021-02-04 01:50

The two pattern seems to achieve the same thing. What are the different use cases in real world? Thanks

相关标签:
7条回答
  • 2021-02-04 02:20

    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.

    0 讨论(0)
  • 2021-02-04 02:21

    They are two entirely different "pattern Types". Singleton is a "CREATION" pattern. Creation patterns are higher order common "Object creation" methodologies. On the other hand, Flyweight is a "Structural pattern". Structural patterns have to do more with interfaces and implementation rather than "object creation". Another easier way to describe the differences is the the first pattern type "Creation patterns" are more noun based. The second "Structural pattern" types are more verb based.

    0 讨论(0)
  • 2021-02-04 02:27

    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.

    0 讨论(0)
  • 2021-02-04 02:30

    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.

    0 讨论(0)
  • 2021-02-04 02:34

    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.

    0 讨论(0)
  • 2021-02-04 02:36

    Don't forget that singleton is a creational pattern, while flyweight is a structural pattern. Hence, the goal of the two patterns is completely different

    0 讨论(0)
提交回复
热议问题