这是什么样的Singleton? [英] Which kind of Singleton is this?
本文介绍了这是什么样的Singleton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class ConnectionManager {
private static Map <String, ConnectionManager> managerInstances = new HashMap<String, ConnectionManager>();
private String dsKey;
private ConnectionManager(String dsKey){
this.dsKey = dsKey;
}
public static ConnectionManager getInstance(String dsKey){
ConnectionManager managerInstance = managerInstances.get(dsKey);
if (managerInstance == null) {
synchronized (ConnectionManager.class) {
managerInstance = managerInstances.get(dsKey);
if (managerInstance == null) {
managerInstance = new ConnectionManager(dsKey);
managerInstances.put(dsKey, managerInstance);
}
}
}
return managerInstance;
}
}
我最近看到这个代码在Singleton模式是不按照GoF的书定义使用。 Singleton正在存储自己的实例的 Map
。
I recently saw this code somewhere where the Singleton pattern was not used as per book definition from GoF. Singleton is storing a Map
of its own instances.
可以调用什么样的单例?或者这是Singleton的有效使用吗?
What kind of singleton can this be called? Or is this a valid use of Singleton?
推荐答案
这不是一个单身人士。它被称为 multiton pattern 。
It's not a singleton. It's called multiton pattern.
而不是每个应用程序使用单个实例 ,所以多重模式可以确保每个密钥的单个实例 。
Rather than have a single instance per application, the multiton pattern instead ensures a single instance per key.
这篇关于这是什么样的Singleton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文