Multiton pattern

The multi-tone ( multi-tone English ) is a generation pattern is used for generating a specified number of objects. It is so to say an extension of the single piece, where only a single object is used. To access the correct object, a unique key is used. The objects and their keys are usually implemented as an associative array in the form of keys and values ​​that are delivered via a static method on request. Thus, there is at most one object is always for each key. If a key is specified for the object does not exist, the object required is generated and made available. Thus, a multi-tone is really nothing more than a group of individual pieces.

Example

A thread- safe Java implementation of a Multitons:

Java

Public class FooMultiton {          private static final Map instances = new HashMap ();            private FooMultiton () / * also acceptable: protected, {default } * / {              / * No explicit implementation * /          }            public static FooMultiton getInstance (Object key ) {              synchronized ( instances ) {                    / / Our "per key" singleton                  FooMultiton instance = instances.get ( key);                    if ( instance == null) {                        / / Lazily create instance                      instance = new FooMultiton ();                        / / Add it to map                      instances.put (key, instance );                  }                    return instance;              }          }            / / Other fields and methods ...        } Clarification of the example

In contrast to a hash table a multitone always returns an object; zero is therefore never returned. Clients can also specify a new key. It allows centralized access to a single directory. However, it goes in contrast with other solutions of the indicated storage ( such as LDAP ) on a single system.

Disadvantages

Unit testing a Multitons are difficult as in the single piece because global variables are used. It can also lead to memory leak if the language supports garbage collection.

Notes

586489
de