这是哪种单身人士?

public class ConnectionManager { private static Map  managerInstances = new HashMap(); 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; } } 

我最近在GoF的书籍定义中没有使用Singleton模式的地方看到了这段代码。 Singleton正在存储自己实例的Map

这可以称为什么样的单身人士? 或者这是Singleton的有效使用?

这不是一个单身人士。 它被称为多重模式 。

而不是每个应用程序只有一个实例,而是确定每个密钥的单个实例。

因为它使用了一个双重检查的锁定习惯用法,这在Java中不是线程安全的,所以这个多声道似乎被打破了。 特别是,当使用非空字符串调用getInstance(s) ,您可以接收指向其dsKey为null的ConnectionManager的非空引用。

使用线程安全的ConcurrentHashMap会更好,并且不需要同步。