我对 Java 相当陌生,并且在以下方面遇到困难:
我有一个创建对象的类,其中保存不同的数据(列表)。数据总是分配给一个标识符(属性):
class History {
private String identifier;
private ArrayList<ObjectsRelatedToIdentifier> objectsRelatedToIdentifier;
}
我现在需要每个标识符(X、Y、Z 等)有一个对象,但只有一个。不可能用标识符 X 实例化两个对象。
此外,我需要能够从其他对象访问这些对象。我如何“捕获”它们?
这些对象的内部结构始终相同,只是标识符不同,当然还有 ArrayList 中的值不同。
是否存在某种“多重单例模式”或不同模式的组合?
最佳答案
Is there some kind of "multiple singleton pattern" or a combination of different patterns?
是的,multiton pattern 。 (来自维基百科)是,
In software engineering, the multiton pattern is a design pattern similar to the singleton, which allows only one instance of a class to be created. The multiton pattern expands on the singleton concept to manage a map of named instances as key-value pairs.
关于java - 创建一个类的多个实例,但根据特定属性保持它们唯一(多个 "singletons"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24860796/