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

前端 未结 8 811
囚心锁ツ
囚心锁ツ 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 00:54

    Besides its place as a particular design pattern, singletons can also be thought of as simply one thing. Its use in programming no doubt derives from its use in mathematics, where a singleton is a set of just one number.

    0 讨论(0)
  • 2020-12-05 00:55

    A singleton is a class of which there can be only one instance in your application. You then share that instance throughout your application.

    Here's a link that might help (covers how to make your singleton thread safe in c# as well):

    Implementing the Singleton Pattern in C#

    0 讨论(0)
  • 2020-12-05 00:57

    There was an excellent Google Tech Talk where the speaker addressed global state (including Singletons) and why they make testing nearly impossible:

    The Clean Code Talks - "Global State and Singletons"

    The speaker makes a distinction between what he calls Singletons (capital 'S'), where steps are taken to prevent the class from being instantiated more than once (i.e., often with a static getInstance method and private constructors), and singletons (small 's') where a single instance is all that is ever needed, but nothing is built into the class to prevent multiple instances from being created.

    0 讨论(0)
  • 2020-12-05 01:04

    Singleton is useful when you must be sure that there is one and only one instance of a class, and that this object must be accessed from multiple locations in the code.

    If it could make sense that more than one instance of your class could be used at once, then you don't want a singleton.

    Here is some information about where to use singletons: http://www.ibm.com/developerworks/webservices/library/co-single.html

    From the article mentioned previously:

    To decide whether a class is truly a singleton, you must ask yourself some questions.

    • Will every application use this class exactly the same way? (exactly is the key word)
    • Will every application ever need only one instance of this class?
      (ever and one are the key words)
    • Should the clients of this class be unaware of the application they are
      part of?

      If you answer yes to all three questions, then you've found a singleton. The key points here are that a class is only a singleton if all applications treat it exactly the same and if its clients can use the class without an application context.

    0 讨论(0)
  • 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).
    0 讨论(0)
  • 2020-12-05 01:09

    A singleton is a class, that can only have a single instance created.

    This is often implemented by having a private constructor, which is assigned to a static variable, and then accessible through a public method.

    There are some problems with using singletons, including:

    • They make testing difficult
    • They cannot be sub-classed
    • Once instantiated, the instance will live forever

    See here for a further description of singleton, and another pattern 'Monostate' that might be useful instead: http://www.objectmentor.com/resources/articles/SingletonAndMonostate.pdf

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