Multiton
Multiton is a software design pattern that generalizes the singleton pattern. Like a singleton, it ensures controlled instantiation, but instead of a single instance, a multiton restricts the number of instances to a finite set, each identified by a unique key. The set of instances is typically stored in a registry or map keyed by the identifier. Access to an instance is through a public accessor, usually named getInstance or obtain, which returns the existing instance for a given key or creates it if it does not yet exist. The class commonly uses a private or protected constructor to prevent arbitrary creation.
Implementation notes: thread safety is a central concern. In concurrent environments, accessors use synchronization or a
private static final Map<String, Multiton> instances = new HashMap<>();
private Multiton(String key) { this.key = key; }
public static synchronized Multiton getInstance(String key) {
Multiton instance = instances.get(key);
}
}
public String getKey() { return key; }
}
Applications include per-tenant configuration objects, per-category loggers, or resource pools limited to a fixed set of
Compared with singleton, multiton trades a single global instance for a small, well-defined set of instances.