Dec 282012

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:


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 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;

    // 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;

    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;

    // Making the destructor protected.
    ~Multiton() {}



 Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Facebook
  • Google+
  • Twitter
  • YouTube