C# 单例设计模式的优势是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12901734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is the advantage of Singleton Design Pattern
提问by Thomas
every one know how to write code for Singleton Design Pattern.say for example
每个人都知道如何为单例设计模式编写代码。例如
public class Singleton
{
// Private static object can access only inside the Emp class.
private static Singleton instance;
// Private empty constructor to restrict end use to deny creating the object.
private Singleton()
{
}
// A public property to access outside of the class to create an object.
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
it is very clear that when we create a instance of any class many time the memory is allocated for each instance but in case of Singleton design pattern a single instance give the service for all calls.
很明显,当我们多次创建任何类的实例时,会为每个实例分配内存,但在单例设计模式的情况下,单个实例为所有调用提供服务。
1) i am bit confuse and really do nor realize that what are the reasons...that when one should go for Singleton Design Pattern. only for saving some memory or any other benefit out there.
1)我有点困惑,真的没有意识到是什么原因......什么时候应该选择单例设计模式。只是为了节省一些内存或任何其他好处。
2) suppose any single program can have many classes then which classes should follow the Singleton Design Pattern? what is the advantage of Singleton Design Pattern?
2) 假设任何单个程序都可以有多个类,那么哪些类应该遵循单例设计模式?单例设计模式的优势是什么?
3 in real life apps when should one make any classes following Singleton Design Pattern? thanks
3 在现实生活中的应用程序中,何时应该遵循单例设计模式创建任何类?谢谢
Here is thread safe singleton
这是线程安全的单例
public sealed class MultiThreadSingleton
{
private static volatile MultiThreadSingleton instance;
private static object syncRoot = new Object();
private MultiThreadSingleton()
{
}
public static MultiThreadSingleton Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
instance = new MultiThreadSingleton();
}
}
}
return instance;
}
}
}
采纳答案by sgud
To assure only one and same instance of object every time.
确保每次只有一个相同的对象实例。
Take a scenario, say for a Company application, there is only one CEO. If you want to create or access CEO object, you should return the same CEO object every time.
假设一个场景,比如说对于 Company 应用程序,只有一个 CEO。如果要创建或访问 CEO 对象,则每次都应返回相同的 CEO 对象。
One more, after logging into an application, current user must return same object every time.
另外,登录应用程序后,当前用户每次都必须返回相同的对象。
回答by djechlin
Generally singleton is considered an anti-pattern in OOP because it means a class is asserting that with respect to the entire program - which in OOP it should have no knowledge of - it knows it's going to be the only one. That being said, singleton is the proper way to implement a constant in my experience. In general if something I was about to hard-code into a program (say a database username) then it can be moved to a Config file or a singleton.
通常单例在 OOP 中被认为是一种反模式,因为它意味着一个类正在断言关于整个程序 - 在 OOP 中它应该不知道 - 它知道它将是唯一的。话虽如此,根据我的经验,单例是实现常量的正确方法。一般来说,如果我要硬编码到程序中(比如数据库用户名),那么它可以移动到配置文件或单例中。
One of few areas Java beats C# (in my opinion...) is its support for enums. Java offers truly OO constants via enums, and so this is how I will always implement a singleton in Java. C# has no ready-equivalent.
Java 击败 C# 的几个方面之一(在我看来......)是它对枚举的支持。Java 通过枚举提供真正的 OO 常量,因此我将始终在 Java 中实现单例。C# 没有现成的等价物。
回答by Furqan Safdar
Benefits of Singleton Pattern:
单例模式的好处:
? Instance control: Singleton prevents other objects from instantiating their own copies of the Singleton object, ensuring that all objects access the single instance.
? 实例控制:Singleton 防止其他对象实例化自己的 Singleton 对象副本,确保所有对象都访问单个实例。
? Flexibility: Since the class controls the instantiation process, the class has the flexibility to change the instantiation process.
? 灵活性:由于类控制了实例化过程,因此类具有改变实例化过程的灵活性。
The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.
Singleton 相对于全局变量的优势在于您在使用 Singleton 时绝对确定实例的数量,并且您可以改变主意并管理任意数量的实例。
回答by Matt Burland
One useful place to use a singleton is if it is accessing some resource that you only want to have a single access point for. For example, I've used it when writing some code to talk to a device. I only want one piece of code talking to the device so I use a singleton. Any attempt to create another instance of the object that talks to the device will just give you the same object back so I never have to worry about two instances maintaining out-of-sync data about the device or getting messages to and from the device mixed up or out-of-order.
使用单例的一个有用的地方是,如果它正在访问某些您只想拥有一个访问点的资源。例如,我在编写一些与设备对话的代码时使用了它。我只想要一段代码与设备对话,所以我使用单例。任何尝试创建与设备对话的对象的另一个实例只会给你相同的对象,所以我永远不必担心两个实例维护有关设备的不同步数据或从设备接收消息混合正常或无序。
But, of course, you are under no obligation to use them. They are just a tool that is sometimes useful.
但是,当然,您没有义务使用它们。它们只是有时有用的工具。
回答by ozgur
Other answers are good, as well. But they are providing examples of behaviouralcharacteristics of the pattern. But, Singleton is more about creation. Thus one of the most important benefit of the pattern is that it is resource friendly. You are not wasting memory for a new objectwhen you actually do not need a new one.
其他答案也很好。但他们提供了模式行为特征的示例。但是,单例更多的是关于创造。因此,该模式最重要的好处之一是它是资源友好的。new object当您实际上不需要新内存时,您不会浪费内存。
This causes another benefit, which is the instantiation overhead is avoided.
这带来了另一个好处,即避免了实例化开销。
回答by Kush Kant Dayal Pune
Real time usages/benefits of Singleton Design Pattern.
单例设计模式的实时用途/好处。
- While using multi-threading, to manage the multi-thread Pool.
- to manage the "service host repositories" in SOA (service oriented architecture).
- for Logging Framework implementation
- in automation Testing/Unit Testing project i.e. Coded UI projects.
- While implementing the Caching in a big application.
- for configuration settings to make proper control over the application.
- 同时使用多线程,来管理多线程Pool。
- 管理 SOA(面向服务的架构)中的“服务主机存储库”。
- 用于日志框架实现
- 在自动化测试/单元测试项目中,即编码的 UI 项目。
- 在大型应用程序中实现缓存时。
- 用于配置设置以对应用程序进行适当的控制。
回答by tagus
It can improve the way that memory is handled in the JVM and with memory being used properly, better performance will be reached. You are not creating multiple objects but trying to create only one, that way, there is less work for the Garbage collector and less memory occupation in the JVM heap.
它可以改进 JVM 中处理内存的方式,并且在正确使用内存的情况下,将达到更好的性能。您不是在创建多个对象,而是尝试只创建一个,这样,垃圾收集器的工作就会减少,JVM 堆中的内存占用也会减少。

