Yeah.. I know.. I'm a simpleton.. So what's a Singleton?

前端 未结 8 840
囚心锁ツ
囚心锁ツ 2020-12-05 00:47

I\'ve tried a few times to understand what a Singleton is. Perhaps I\'m just too visual.. so can anyone break it down in a simple analogy.

Similar Posts:

8条回答
  •  囚心锁ツ
    2020-12-05 01:07

    As requested, here are a few analogies:

    • The Earth
    • The Universe
    • The element oxygen (there are other elements, but only one oxygen. There are lots of oxygen molecules, but only one canonical oxygen element.)
    • The concept of True
    • The concept of False

    You could instantiate lots of True objects, but they will all refer to the same actual item (i.e. the universal concept of True). Depending on your application's domain, there may be more specific examples:

    • The database connection
    • The application's main thread
    • Anything that represents a hardware device (i.e. you only want to instantiate one object representing CPU0).

提交回复
热议问题