What are the real world applications of the singleton pattern?

后端 未结 10 586
南旧
南旧 2021-02-01 07:22

Duplicate

On design patterns: When should I use the singleton?

class Singleton
{
    private static Singleton instance;

    private Singleton() {}

             


        
10条回答
  •  攒了一身酷
    2021-02-01 07:52

    You could find the singleton pattern useful if you have a resource that costs a lot to get initialized, and that you use several times, such as an object context when you use an ORM or a connection of some sort.

    Obviously you must pay attention that keeping that thing alive does not cost you more than recreating it every time.

提交回复
热议问题