java 我们可以为一个类只创建一个 Single 对象吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7703632/
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
Can we create only a Single object for a class
提问by Harini
Is it possible to create only single object to the class?. How to do that in Java?
是否可以只为类创建单个对象?。如何在 Java 中做到这一点?
回答by Hamed
The following class definition is a Singleton Pattern:
以下类定义是单例模式:
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
The only way to retrieve an instance of the Singleton
object for this class is to call the getInstance()
method which insures that there will always be one instance of the Singleton
object.
Singleton
为此类检索对象实例的唯一方法是调用getInstance()
确保始终存在Singleton
对象实例的方法。
回答by óscar López
As the other answers suggest, the Singleton Patterncan guide your implementation so you create only a single instance of a class ... or not.
正如其他答案所暗示的那样,单例模式可以指导您的实现,因此您只创建一个类的单个实例......或不创建。
Truth is, many factors come into play, in particular if you're working under a managed environment, like an application server on top of a Java virtual machine. In that case, there can be multiple servers in a cluster, each server can start multiple JVMs, and each one can start multiple classloaders, each one of them creating a differentinstance of the "singleton" class. So, there is no way to guarantee that a single instance of an object will exist, for a given class!
事实是,有很多因素会起作用,特别是如果您在托管环境下工作,例如 Java 虚拟机上的应用程序服务器。在这种情况下,集群中可以有多个服务器,每个服务器可以启动多个 JVM,每个服务器可以启动多个类加载器,每个类加载器创建“单例”类的不同实例。因此,对于给定的类,无法保证对象的单个实例将存在!
Besides, many people thinks that singletons are evil, and you should consider carefully if you really need to use one, and whether your environment will allow it.
此外,很多人认为单例是邪恶的,你应该仔细考虑是否真的需要使用单例,以及你的环境是否允许。
回答by Peter Lawrey
The simplest way to do this is to use an enum
最简单的方法是使用枚举
enum Singleton {
INSTANCE;
}
The class is lazy loaded and thread safe.
该类是延迟加载和线程安全的。
回答by sbrajesh
You should look at the Singleton Pattern
你应该看看单例模式
If you can provide details about language, It would be easy to provide an example.
如果您可以提供有关语言的详细信息,则很容易提供示例。
回答by ayckoster
Depends. Usually creating only one object of a class smells of Singleton pattern.
依靠。通常只创建一个类的一个对象有单例模式的味道。
Either you do not create any objects and use only class variables and methods or you use a proxy method.
要么不创建任何对象,只使用类变量和方法,要么使用代理方法。
Something like:
就像是:
public static Object getInstance(){
if(self.instance == null) {
self.instance = new Object();
}
return self.instance;
}
Instead of calling new Object() in client side code you only call getIntstance();
而不是在客户端代码中调用 new Object() 你只调用 getIntstance();
回答by eric
If you're asking how to implement the Singleton pattern, I recently came across an excellent article, Implementing Singleton in C#, which gives code examples and covers some potential issues.
如果您要问如何实现单例模式,我最近看到了一篇出色的文章,在 C# 中实现单例,其中提供了代码示例并涵盖了一些潜在问题。