问题描述
我已经尝试过几次了解单身人士.也许我太视觉了.
类似的帖子:
- 初始化Singletons的不同方法
- singleton:应该如何使用
- ,这是对Singleton模式的很好使用吗?<<<<<<<<
- Singletons有什么糟糕的?
- singleton:应该如何使用
- singletons:良好的设计或拐杖?
- in .net in .net global vs singleton
- on Design模式:何时使用Singleton?
- singleton在java中带有参数
- 在Java中实现Singleton模式的有效方法是什么?
- Singleton的替代方案
- 滥用Singleton class的最常见示例 班级
- 一个>
- 使用Singleton vs single vs in .net call in .net export?
- 什么是单身班?它可以帮助我运行两个相关服务的类的单个实例吗?
推荐答案
根据要求,这里有一些类比:
- 地球
- 宇宙
- 元素氧(还有其他元素,但只有一个氧.
- 真实的概念
- false的概念
您可以实例化许多真实对象,但它们都会指代相同的实际项目(即真实的通用概念).根据您的应用程序域,可能有更多具体的示例:
- 数据库连接
- 应用程序的主线程
- 任何代表硬件设备的东西(即,您只想实例化代表CPU0的对象).
其他推荐答案
单身人士是一类,您的应用程序中只能有一个实例.然后,您在整个应用程序中共享该实例.
这是一个可能会有所帮助的链接(涵盖了如何在C#中确保单身线程安全):
中实现Singleton模式其他推荐答案
当您必须必须确保有一个和的一个一个一个实例时,单身人士很有用必须从代码中的多个位置访问对象.
如果可以一次使用您的班级的一个以上实例,那么您就不需要单身.
这是有关在哪里使用单例的一些信息: co-single.html
从前面提到的文章中:
决定班级是否真的是 辛格尔顿,你必须问自己一些 问题.
- 每个应用程序都会以完全相同的方式使用此类? (正是关键词)
- 每个应用程序都只需要此类的一个实例吗?
(永远是关键词)本班的客户是否应该不知道
的一部分
的应用程序 ?如果您回答所有三个问题,那么您找到了一个 辛格尔顿.这里的要点是 如果一堂课只是单身人士 所有应用程序都准确对待 同样以及客户是否可以使用 没有应用程序上下文的课程.
问题描述
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 (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).
其他推荐答案
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#
其他推荐答案
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.