Multiton

Limits the number of object instances that can be created.



Usage in JavaScript:
low


Fundamentals


The Multiton pattern is from the same lineage as the Singleton pattern. Singleton limits the number of object instances to one and Multiton limits it to any given number. To get a better appreciation for Multiton you may want to review the Singleton pattern which is listed in the Classic Patterns section.

Two varieties of Multiton exist. The first one is similar to Singleton, but instead of limiting the number of concurrent instances to 1 it limits it to n, where n is a number you specify. We will call this N-ton. The second variety of Multiton is a map (or dictionary) of Singletons, where each can be retrieved by a key. Essentially these are name/value pairs where named instances of Singletons are stored. This last construct is sometimes referred to as a Registry of Singletons. We will look at each one.

The N-ton model is helpful when, for whatever reason, you need to limit the number of instances. This is usually related to some limited resource (such as a server or a database) that you are trying to manage. Here is the code for the N-ton model:

var Multiton = function (limit) {
    var limit = limit;
    var instances = [];

    var Instance = function () {
        // instance code here
        this.say = function () {
             alert("I am an instance" );
        };
    };

    return {
        getInstance: function () {
            if (instances.length === 0) {
                for (var i = 0; i < limit; i++) {
                    instances.push(new Instance());
                }
            }
            var random = Math.floor(Math.random() * limit);
            return instances[random];
        }
    };
};

var multiton = new Multiton(6);

var m1 = multiton.getInstance();
var m2 = multiton.getInstance();
var m3 = multiton.getInstance();
var m4 = multiton.getInstance();

m1.say();       // => I am an instance
m2.say();       // => I am an instance

Run

In this example the Multiton manages a maximum of 6 instances. When an instance is requested with getInstance a randomly selected instance is returned. Notice that the constructor function named Instance is privately scoped within Multiton and can only be called from within Multiton. The Instance constructor is where you specify instance specific properties and methods.

An example of the Registry of Singletons variety of the Multiton pattern is demonstrated next.

var Multiton = function () {
    var instances = {};

    var Instance = function (id) {
        // instance code here
        this.id = id;
        this.say = function () {
             alert("Instance for Server " + this.id );
        };
    };

    return {
        getInstance: function (id) {
            if (Object.keys(instances).length === 0) {
                for (var i = 1; i < 5; i++) {
                    instances[i] = new Instance(i);
                }
            }
            return instances[id];
        }
    };
};

var multiton = new Multiton();

var server1 = multiton.getInstance(1);
var server2 = multiton.getInstance(2);
var server3 = multiton.getInstance(3);
var server4 = multiton.getInstance(4);

server1.say();       // => Instance for Server 1
server3.say();       // => Instance for Server 3
Run

This code is quite similar to the previous variety only that we ask for specific instances that are keyed by a unique id (hardcoded from 1 to 4). \ Each singleton represents a server. The instances private object maintains the name/value pairs (in the N-ton implementation this was an array).



  Lazy Load
Partial