Multitone

from Wikipedia, the free encyclopedia
UML diagram of a multitone

The multiton ( English multiton ) is a generation pattern in computer science that is used to generate a certain number of objects. The Gang of Four calls it flyweight (English flyweight ). It's like an extension of the singleton where only a single object is used. A unique key is used to access the correct object . The objects and their keys are mostly implemented as associative data fields in the form of keys and values, which can be delivered on request using a static method. So there is always at most one object for each key. If a key is specified for which the object does not exist, the required object is generated and made available. As a result, a multitone is actually nothing more than a group of individual pieces.

example

A thread-safe implementation of a multiton in Java :

public class FooMultiton {
    private static final Map<Object, FooMultiton> instances = new HashMap<Object, FooMultiton>();

    private FooMultiton() /* also acceptable: protected, {default} */ {
        // ...
    }

    public static FooMultiton getInstance(Object key) {
        synchronized (instances) {

            // Zu key gehörige Instanz aus Map holen
            FooMultiton instance = instances.get(key);

            if (instance == null) {
                // Lazy Creation, falls keine Instanz gefunden
                instance = new FooMultiton();

                instances.put(key, instance);
            }

            return instance;
        }
    }

    // Weitere Felder und Methoden ...
}

In contrast to a hash table , a multitone always returns an object; nullso it is never returned. Clients cannot set new keys either. It allows centralized access to a single directory. In contrast to other solutions for indexed storage (such as LDAP ), however, it runs on a single system.

disadvantage

As with the singleton , unit tests of a multitone are difficult because global variables are used. Memory leaks can also occur when the language uses garbage collection because it maintains strong global references to the objects.

Web links

Individual evidence

  1. ^ Richard Carr: Multitone Design Pattern. The multiton design pattern is an extension of the singleton pattern. It ensures that a limited number of instances of a class can exist by specifying a key for each instance and allowing only a single object to be created for each of those keys. BlackWasp, March 3, 2012; archived from the original on March 3, 2012 ; retrieved on September 21, 2012 (English): “The multiton design pattern is very similar to the singleton. When a request is made for an object, a key is passed to a static method that will generate or return one. If the key has not been used before, a new object is instantiated, linked to the key and returned. If the key has been used before, the object previously linked to that key is returned. Essentially, the multiton provides the functionality of a group of singletons. "
  2. googletesting.blogspot.com