Content deleted Content added
m →Drawbacks: HTTP to HTTPS for Blogspot |
|||
(22 intermediate revisions by 18 users not shown) | |||
Line 1:
{{Short description|Software engineering design pattern}}
In [[software engineering]], the '''multiton pattern''' is a [[design pattern (computer science)|design pattern]]
Rather than having a single instance ''per application'' (e.g. the {{Javadoc:SE|package=java.lang|java/lang|Runtime}} object in the [[
The multiton pattern does not explicitly appear as a pattern in the highly regarded [[object-oriented programming]] textbook ''[[Design Patterns]]''.<ref>{{cite book |last1=O'Docherty |first1=Mike |title=Object-oriented analysis and design: understanding system development with UML 2.0 |date=2005 |publisher=Wiley |___location=Chichester |isbn=0470092408 |page=341}}</ref> However, the book describes using a '''registry of singletons''' to allow subclassing of singletons,<ref>{{cite book |title=Design patterns: elements of reusable object-oriented software |date=2011 |publisher=Addison-Wesley |___location=Boston, Mass. Munich |isbn=0-201-63361-2 |page=130}}</ref> which is essentially the multiton pattern.{{Citation needed|date=April 2012}}
==
While it may appear that the multiton is
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 (data structure)|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 (computer science)|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,<ref>
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:
== References ==▼
<syntaxhighlight lang="c#" line="1">
using System;
using System.Collections.Generic;
public enum MultitonType
{
Zero,
One,
Two
}
public class Multiton
{
private static readonly Dictionary<MultitonType, Multiton> instances =
new Dictionary<MultitonType, Multiton>();
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 var instance))
{
instance = new Multiton(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);
}
}
</syntaxhighlight>
{{Reflist}}
==External links==
* [https://rubygems.org/gems/multiton/versions/0.0.1 Multiton implementation in Ruby language]
* [https://github.com/PureMVC/puremvc-as3-multicore-framework/blob/master/src/org/puremvc/as3/multicore/patterns/facade/Facade.as
* [http://gen5.info/q/2008/07/25/the-multiton-design-pattern/ Article with a C# Multiton implementation, example of use, and discussion of memory issues]
{{Design Patterns patterns}}
[[Category:Software design patterns]]
[[Category:Articles with example Java code]]
|