Multiton pattern

From Wikipedia, the free encyclopedia
  (Redirected from Multiton)
Jump to: navigation, search
UML diagram of the multiton

In software engineering, the multiton pattern is a design pattern similar to the singleton, which allows only one instance of a class to be created. The multiton pattern expands on the singleton concept to manage a map of named instances as key-value pairs.

Rather than having a single instance per application (e.g. the java.lang.Runtime object in the Java programming language) the multiton pattern instead ensures a single instance per key.

Most people and textbooks consider this a singleton pattern[citation needed]. For example, multiton does not explicitly appear in the highly regarded object-oriented programming textbook Design Patterns (it appears as a more flexible approach named registry of singletons).


While it may appear that the multiton is no more than a simple hash table with synchronized access there are two important distinctions. First, the multiton does not allow clients to add mappings. Secondly, the multiton never returns a null or empty reference; instead, it creates and stores a multiton instance on the first request with the associated key. Subsequent requests with the same key return the original instance. A hash table is merely an implementation detail and not the only possible approach. The pattern simplifies retrieval of shared objects in an application.

Since the object pool is created only once, being a member associated with the class (instead of the instance), the multiton retains its flat behavior rather than evolving into a tree structure.

The multiton is unique in that it provides centralized access to a single directory (i.e. all keys are in the same namespace, per se) of multitons, where each multiton instance in the pool may exist having its own state. In this manner, the pattern advocates indexed storage of essential objects for the system (such as would be provided by an LDAP system, for example). However, a multiton is limited to wide use by a single system rather than a myriad of distributed systems.


This pattern, like the Singleton pattern, makes unit testing far more difficult,[1] as it introduces global state into an application.

With garbage collected languages it may become a source of memory leaks as it introduces global strong references to the objects.


In Java, the multiton pattern can be implemented using an enumerated type, with the values of the type corresponding to the instances. In the case of an enumerated type with a single value, this gives the singleton pattern.

In C#, we can also use enums, as the following example shows:

 1 using System.Collections.Generic;
 3 public enum MultitonType {
 4     ZERO, 
 5     ONE, 
 6     TWO
 7 };
 9 public class Multiton {
10     private static readonly Dictionary<MultitonType, Multiton> instances =
11         new Dictionary<MultitonType, Multiton>();
12     private int number;
14     private Multiton(int number) {
15         this.number = number;
16     }
18     public static Multiton GetInstance(MultitonType type) {
19         // lazy init (not thread safe as written)
20         // Recommend using Double Check Locking if needing thread safety
21         if (!instances.ContainsKey(type)) {
22             instances.Add(type, new Multiton((int)type));
23         }
24         return instances[type];
25     }
27     public override string ToString() {
28         return "My number is " + number.ToString();
29     }
31     // Sample usage
32     public static void Main(string[] args) {
33         Multiton m0 = Multiton.GetInstance(MultitonType.ZERO);
34         Multiton m1 = Multiton.GetInstance(MultitonType.ONE);
35         Multiton m2 = Multiton.GetInstance(MultitonType.TWO);
36         System.Console.WriteLine(m0);
37         System.Console.WriteLine(m1);
38         System.Console.WriteLine(m2);
39     }
40 }


External links[edit]