Multiton pattern
In software engineering, the multiton pattern is a design pattern which generalizes the singleton pattern. Whereas the singleton allows only one instance of a class to be created, the multiton pattern allows for the controlled creation of multiple instances, which it manages through the use of a map.
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.
The multiton pattern does not explicitly appear as a pattern in the highly regarded object-oriented programming textbook Design Patterns.[1] However, the book describes using a registry of singletons to allow subclassing of singletons,[2] which is essentially the multiton pattern.[citation needed]
Description
While it may appear that the multiton is a 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.
Drawbacks
This pattern, like the Singleton pattern, makes unit testing far more difficult,[3] 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.
Implementations
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:
namespace Wikipedia.Examples;
using System;
using System.Collections.Generic;
public enum MultitonType
{
Zero,
One,
Two
}
public class Multiton
{
private static readonly Dictionary<MultitonType, Multiton> instances = new();
private MultitonType type;
private Multiton(MultitonType type)
{
this.type = type;
}
public static Multiton GetInstance(MultitonType type)
{
// Lazy init (not thread safe as written)
// Recommend using Double Check Locking if needing thread safety
if (!instances.TryGetValue(type, out Multiton instance))
{
instance = new(type);
instances.Add(type, instance);
}
return instance;
}
public override string ToString()
{
return $"My type is {this.type}";
}
// Sample usage
public static void Main(string[] args)
{
Multiton m0 = Multiton.GetInstance(MultitonType.Zero);
Multiton m1 = Multiton.GetInstance(MultitonType.One);
Multiton m2 = Multiton.GetInstance(MultitonType.Two);
Console.WriteLine(m0);
Console.WriteLine(m1);
Console.WriteLine(m2);
}
}
References
- ^ O'Docherty, Mike (2005). Object-oriented analysis and design: understanding system development with UML 2.0. Chichester: Wiley. p. 341. ISBN 0470092408.
- ^ Design patterns: elements of reusable object-oriented software. Boston, Mass. Munich: Addison-Wesley. 2011. p. 130. ISBN 0-201-63361-2.
- ^ "Clean Code Talks - Global State and Singletons".
External links
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.