Object pool design pattern
December 20, 2012
Find non repeating number
December 28, 2012

Multiton design pattern

Multiton is a Creational design pattern. It is just an extension of Singleton Pattern.
Singleton allows creation of only one object of the class. The constructor is made private and objects are created only thru a static function which returns instance of the object. When this function is called first time, a new object of the class is created and its reference is stored in the class. Subsequent call to the function returns the same object (whose reference is stored).
Hence only one object gets created, while the multiton allows keeping multiple instances (exactly n instances, where n is defined) by maintaining a map of related keys and unique objects.
Note that there can be only one instance per key when implementing the multiton pattern. Also, note that the key does not have to be a string value.
While requesting for an object, you need to pass a key (to the static method which returns the object).
The method will check, if an object corresponding to the key is already generated then that obejct is returned, else a new object is created corresponding to that key and that object is linked with the key and returned.
Essentially, the multiton provides the functionality of a group of singletons. One object per key (rather than one object per application)
Class Diagram:

multiton

It is similar to Singleton.

The default constructor is made private, to restrict direct creation of object. In case of C++, the Copy Constructor and overloaded assignment operator should also be made private (as described in the Singleton design pattern).

A private static dictionary is maintained which keep <key, value> pair where key can be any user defined key and value is the object of MultitonClass class.

static method to create instance of object corresponding to a particular key. Initially the values for all the keys will be all null and when object is asked corresponding to a particular key for the first time, then object of class will be created and stored in the dictionary. When object corresponding to that key is asked subsequently, then the same object (created earlier) is returned (and hence only one object gets created for a particular key).

Example Code:

/**
 * MultitonClass Design pattern.
 */
class MultitonClass
{
  private:
    // Private Constructors & assignment operator.
    Multiton() {}
    Multiton(const Multiton&) {}
    Multiton& operator = (const Multiton&) { return *this; }
    // Dictionary to keep key-value pair. We are keeping the key as string
    static std::map<string, MultitonClass*> _instance;
public:
    // function to destroy all the obejcts.
    // It is like the destructor of the class.
    static void destroy()
    {
        for (typename std::map<Key, T*>::const_iterator it = _instance.begin(); it != _instance.end(); ++it)
            delete (*it).second;
        _instance.clear();
    }
    static MultitonClass* getInstance(const Key& key)
    {
        typename std::map<string, MultitonClass*>::const_iterator it = _instance.find(key);
        // return the object if it exist for the given key
        if (it != _instance.end()) {
            return (MultitonClass*)(it->second);
        }
        // Else create a new object and return its instance.
        MultitonClass* instance = new MultitonClass();
        _instance[key] = instance;
        return instance;
    }
protected:
    // Making the destructor protected.
    ~Multiton() {}
};

 
 

3 Comments

  1. Rakesh Kumar says:

    Hi Kamal,
    I am following your blog. It is very beneficial for me.
    Thanks a lot. 🙂
    I need one help from you.I am not getting the proper and correct answer.
    My query is Can we delete the singleton object in singleton pattern.
    If yes how can we delete it in C++. If possible please share the code with me.
    It would be really nice.
    Thanks,
    Rakesh Kumar

  2. Rakesh Kumar says:

    Hi Kamal, I am waiting for your response pls.

  3. sogand says:

    hi
    what is the output of this example of multiton pattern??

Leave a Reply

Your email address will not be published. Required fields are marked *