如何确保JVM中只有一个类实例? [英] How to make sure that there is just one instance of class in JVM?
问题描述
我正在开发一个设计模式,我想确保这里只是Java虚拟机中一个类的一个实例,通过一个点汇集一些资源的所有请求,但我不知道它是否是可能的。
I am developing a design pattern, and I want to make sure that here is just one instance of a class in Java Virtual Machine, to funnel all requests for some resource through a single point, but I don't know if it is possible.
我只想到一种计算类实例的方法,并在创建第一个实例后销毁所有实例。
I can only think of a way to count instances of a class and destroy all instance after first is created.
这是一种正确的方法吗?如果没有,还有其他方法吗?
Is this a right approach? If not, is there any other way?
推荐答案
使用 singleton 模式。最简单的实现包括私有构造函数
和字段
来保存其结果,以及 static
访问器方法,名称类似于 getInstance()
。
Use the singleton pattern. The easiest implementation consists of a private constructor
and a field
to hold its result, and a static
accessor method with a name like getInstance()
.
私有字段可以在静态初始化程序块中分配,或者更简单地使用初始化程序分配。 getInstance()
方法(必须是公共的)然后只返回此实例,
The private field can be assigned from within a static initializer block or, more simply, using an initializer. The getInstance()
method (which must be public) then simply returns this instance,
public class Singleton {
private static Singleton instance;
/**
* A private Constructor prevents any other class from
* instantiating.
*/
private Singleton() {
// nothing to do this time
}
/**
* The Static initializer constructs the instance at class
* loading time; this is to simulate a more involved
* construction process (it it were really simple, you'd just
* use an initializer)
*/
static {
instance = new Singleton();
}
/** Static 'instance' method */
public static Singleton getInstance() {
return instance;
}
// other methods protected by singleton-ness would be here...
/** A simple demo method */
public String demoMethod() {
return "demo";
}
}
请注意使用懒惰评估的方法 getInstance()
方法(设计模式中提倡
)在Java中不是必需的,因为Java已经使用lazy
loading。单例类可能不会被加载,除非它的 getInstance()
被调用,所以没有必要尝试推迟单例构造,直到它需要
通过 getInstance()
测试 null
的单例变量并在那里创建单例
。
Note that the method of using "lazy evaluation" in the getInstance()
method (which
is advocated in Design Patterns), is not necessary in Java because Java already uses "lazy
loading." Your singleton class will probably not get loaded unless its getInstance()
is called, so there is no point in trying to defer the singleton construction until it’s needed
by having getInstance()
test the singleton variable for null
and creating the singleton
there.
使用这个类同样简单:只需获取并保留引用,并在其上调用方法:
Using this class is equally simple: simply get and retain the reference, and invoke methods on it:
public class SingletonDemo {
public static void main(String[] args) {
Singleton tmp = Singleton.getInstance();
tmp.demoMethod();
}
}
有些评论员认为单身人士也应该提供公开final
clone()
只抛出异常的方法,以避免作弊的子类和
clone()
单身人士。但是,显然只有一个私有构造函数
的类不能被子类化,所以这种偏执似乎不是必需的。
Some commentators believe that a singleton should also provide a public final
clone()
method that just throws an exception, to avoid subclasses that "cheat" and
clone()
the singleton. However, it is clear that a class with only a private constructor
cannot be subclassed, so this paranoia does not appear to be necessary.
这篇关于如何确保JVM中只有一个类实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!