On design patterns: When should I use the singleton?

后端 未结 20 3002
失恋的感觉
失恋的感觉 2020-11-22 02:45

The glorified global variable - becomes a gloried global class. Some say breaking object-oriented design.

Give me scenarios, other than the good old logger where it

相关标签:
20条回答
  • 2020-11-22 03:43

    You can use Singleton when implementing the State pattern (in the manner shown in the GoF book). This is because the concrete State classes have no state of their own, and perform their actions in terms of a context class.

    You can also make Abstract Factory a singleton.

    0 讨论(0)
  • 2020-11-22 03:44

    I think singleton use can be thought of as the same as the many-to-one relationship in databases. If you have many different parts of your code that need to work with a single instance of an object, that is where it makes sense to use singletons.

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