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

走远了吗. 提交于 2019-12-17 15:39:16

问题


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:

  • Different ways to initialize singletons
  • Singleton: How should it be used
  • Is this a good use of the Singleton pattern?
  • What is so bad about singletons?
  • Singleton: How should it be used
  • Singletons: good design or a crutch?
  • Global vs Singleton in .NET
  • On Design Patterns: When to use the Singleton?
  • Singleton with Arguments in Java
  • What is an efficient way to implement a singleton pattern in Java?
  • What's Alternative to Singleton
  • Most common examples of misuse of singleton class class
  • https://stackoverflow.com/questions/1395766/asp-net-objectdatasource-singleton
  • Using Singleton vs Single Call in .NET Remoting?
  • what is a singleton class? Can it help me running single instance of a class for two related services?

回答1:


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).



回答2:


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#




回答3:


A singleton is a global variable in sheep's clothing :)

http://googletesting.blogspot.com/2008/08/root-cause-of-singletons.html




回答4:


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.




回答5:


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




回答6:


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.




回答7:


Singleton sometimes can be not so single. Take a look here: When a singleton is not a Singleton?, article provided by Sun.

One recommendation: Don't use it. It's just not good, can create serious performance bottlenecks in your application, and the effort to test singleton classes is just not worth it.

From the above mentioned Wikipedia article:

It should be noted that this pattern makes unit testing far more difficult, as it introduces Global state into an application.

Another resource worth looking at is this post on the google testing blog.




回答8:


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.



来源:https://stackoverflow.com/questions/726685/yeah-i-know-im-a-simpleton-so-whats-a-singleton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!