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

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

提交回复
热议问题