Explore the Multiton Pattern in TypeScript, a design pattern that extends the Singleton Pattern by allowing controlled creation of multiple instances, each identified by a key.
In the world of software design patterns, the Multiton Pattern stands out as a powerful extension of the Singleton Pattern. While the Singleton Pattern ensures that a class has only one instance, the Multiton Pattern allows for the controlled creation of multiple instances, each identified by a unique key. This pattern is particularly useful in scenarios where a fixed number of instances are required, and each instance must be distinguished by a key.
The Multiton Pattern is a creational design pattern that manages a map of named instances. Unlike the Singleton Pattern, which restricts the instantiation of a class to a single object, the Multiton Pattern allows for multiple instances, each associated with a unique key. This pattern ensures that a class maintains a collection of instances, each accessible through a key.
The Multiton Pattern addresses the need for managing a fixed number of instances that are distinguished by keys. This is particularly useful in scenarios where:
Let’s dive into the implementation of the Multiton Pattern in TypeScript. We’ll start by defining a class that manages a collection of instances, each identified by a key.
1class Multiton {
2 private static instances: Map<string, Multiton> = new Map();
3
4 private constructor(private key: string) {}
5
6 public static getInstance(key: string): Multiton {
7 if (!Multiton.instances.has(key)) {
8 Multiton.instances.set(key, new Multiton(key));
9 }
10 return Multiton.instances.get(key)!;
11 }
12
13 public getKey(): string {
14 return this.key;
15 }
16}
17
18// Usage
19const instanceA = Multiton.getInstance('A');
20const instanceB = Multiton.getInstance('B');
21const anotherInstanceA = Multiton.getInstance('A');
22
23console.log(instanceA === anotherInstanceA); // true
24console.log(instanceA === instanceB); // false
instances) is used to store instances, with keys as identifiers.getInstance method with a key, ensuring that each key corresponds to a unique instance.To better understand the Multiton Pattern, let’s visualize the relationship between keys and instances using a class diagram.
classDiagram
class Multiton {
- Map~string, Multiton~ instances
- string key
+ getInstance(key: string): Multiton
+ getKey(): string
}
Multiton --> "1" Map : manages
Multiton --> "1" string : key
Now that we’ve explored the Multiton Pattern, try modifying the code example to add additional functionality. For instance, you could:
For more information on design patterns, consider exploring the following resources:
Before we wrap up, let’s reinforce what we’ve learned with a few questions:
The Multiton Pattern is a powerful extension of the Singleton Pattern, allowing for the controlled creation of multiple instances, each identified by a key. By centralizing instance management and providing keyed access, the Multiton Pattern addresses the need for managing a fixed number of instances efficiently. As you continue your journey in software design, consider how the Multiton Pattern can be applied to solve complex problems in your applications.
Remember, this is just the beginning. As you progress, you’ll build more complex and interactive applications. Keep experimenting, stay curious, and enjoy the journey!