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?
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 (ie 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 (ie you only want to instantiate one object representing CPU0).
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#
A singleton is a global variable in sheep's clothing :)
http://googletesting.blogspot.com/2008/08/root-cause-of-singletons.html
链接地址:
http://www.djcxy.com/p/50084.html
上一篇:
C ++中的安全单例
下一篇:
是啊..我知道..我是个傻瓜。那么什么是单身?