The Multiton pattern is a variation of the Singleton pattern. While the Singleton ensures that only one instance of a class is created, the Multiton pattern ensures that there are only a limited number of instances of a class and provides a global point of access to these instances.
Here's a detailed example in C#:
Dictionary to store instances: The static dictionary instances
keeps track of all the created instances by their identifier.
Private Constructor: The constructor is private to prevent direct instantiation.
GetInstance Method: This method is used to get an instance of the class. If an instance with the given identifier doesn't exist, it creates a new one and stores it in the dictionary.
DoWork Method: This is a simple method to demonstrate that the instances are unique based on their identifier.
In this way, the Multiton pattern controls the number of instances and provides a way to manage them by using unique identifiers.
If you have any specific use-cases or more questions about the Multiton pattern, feel free to ask!