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

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

提交回复
热议问题