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

后端 未结 7 473
攒了一身酷
攒了一身酷 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: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.

提交回复
热议问题