Multiton Design Pattern
Multiton Design Pattern in C++: A Simple Explanation
The Multiton design pattern is a variation of the Singleton pattern, which allows for multiple instances of a class to be created based on a given key or identifier. Each instance is stored in a map or similar data structure, and can be accessed by providing the corresponding key. This allows for the creation of a limited number of instances of a class, each with a unique identifier, and provides a convenient way to manage them.
Steps to implement Multiton Design Pattern:
The following steps can be followed to implement the Multiton design pattern:
- Create a class with a private constructor, so that instances cannot be created from outside the class.
- Declare a static local map to store instances of the class, using the key as the index and the value as a pointer to the instance.
- Declare a public static method to retrieve an instance of the class, based on the given key.
- In the method, check if an instance with the given key already exists in the map. If it does, return the existing instance. If not, create a new instance, store it in the map, and return it.
- Declare the copy constructor, move constructor, and assignment operator as private and undefined, to prevent cloning or moving of instances.
- Declare a public static method to delete all the instances of map.
Implementing the Multiton Design Pattern in C++:
Get CodeDesignHub’s stories in your inbox
Join Medium for free to get updates from this writer.
Source Code:
#include <unordered_map>
#include <iostream>
using namespace std;
class Multiton {
private:
static std::unordered_map<int, Multiton*> instances;
Multiton() {}
public:
static Multiton* getInstance(int key) {
if (instances.find(key) == instances.end()) {
Multiton* instance = new Multiton();
instances[key] = instance;
}
return instances[key];
}
static void destroy() {
for(auto it= instances.begin(); it!=instances.end(); it++){
delete it->second;
}
instances.clear();
}
Multiton(const Multiton&) = delete;
Multiton& operator=(const Multiton&) = delete;
Multiton(Multiton&&) = delete;
Multiton& operator=(Multiton&&) = delete;
};
std::unordered_map<int, Multiton*> Multiton::instances; // Initialize static map variable
int main() {
Multiton* instance1 = Multiton::getInstance(1);
Multiton* instance2 = Multiton::getInstance(2);
Multiton* instance3 = Multiton::getInstance(1);
if(instance1 == instance3){
cout<<"Both the instances are same!"<<endl;
} else {
cout<<"Instances are not same!"<<endl;
}
// instance1 and instance3 should be the same object
// instance2 should be a different object
return 0;
}In the above code, the Multiton class has a private constructor, so that no other object can create an instance of the class. The getInstance() method is a static method that takes a key parameter, which is used to identify the instance of the class. The std::unordered_map is used to store the instances of the Multiton class, where the keys are the unique identifiers. If an instance with the given key already exists, the getInstance() method returns the existing instance, otherwise it creates a new instance and adds it to the map.
The Multiton class also has the deleted copy and move constructors and assignment operators to prevent copying and moving of the Multiton object.
In the main() function, we create three instances of the Multiton class using different keys. Since we already created an instance with the key “key1”, the getInstance() method returns the same instance for both instance1 and instance3.
The implementation of the Multiton pattern in C++ can be done using a std::map or a std::unordered_map to store the instances of the class.
Conclusion
The Multiton design pattern provides a way to limit the number of instances of a class and provide a unique identifier for each instance. This can be useful in cases where multiple instances of a class are required, but limited in number. The implementation involves creating a private constructor, a static map to store instances, and a public static method to retrieve instances based on a key. The copy and move constructors and assignment operator should also be deleted to prevent cloning or moving of instances.
Notes:
The instances of the class are not destroyed when the program ends, so it is important to ensure that they are properly managed.






