<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Multiton_pattern</id>
	<title>Multiton pattern - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Multiton_pattern"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Multiton_pattern&amp;action=history"/>
	<updated>2026-05-04T15:22:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Multiton_pattern&amp;diff=2275925&amp;oldid=prev</id>
		<title>imported&gt;Jlwoodwa: unpiped links using script</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Multiton_pattern&amp;diff=2275925&amp;oldid=prev"/>
		<updated>2024-07-01T05:15:27Z</updated>

		<summary type="html">&lt;p&gt;unpiped links using &lt;a href=&quot;/wiki143/index.php?title=User:Nardog/Unpipe&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;User:Nardog/Unpipe (page does not exist)&quot;&gt;script&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Software engineering design pattern}}&lt;br /&gt;
{{Plain image with caption|Image:Multiton.svg|UML diagram of the multiton}}&lt;br /&gt;
In [[software engineering]], the &amp;#039;&amp;#039;&amp;#039;multiton pattern&amp;#039;&amp;#039;&amp;#039; is a [[design pattern (computer science)|design pattern]] which generalizes the [[singleton pattern]]. Whereas the [[singleton pattern | 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 [[associative array|map]].&lt;br /&gt;
&lt;br /&gt;
Rather than having a single instance &amp;#039;&amp;#039;per application&amp;#039;&amp;#039; (e.g. the {{Javadoc:SE|package=java.lang|java/lang|Runtime}} object in the [[Java programming language]]) the multiton pattern instead ensures a single instance &amp;#039;&amp;#039;per key&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The multiton pattern does not explicitly appear as a pattern in the highly regarded [[object-oriented programming]] textbook &amp;#039;&amp;#039;[[Design Patterns]]&amp;#039;&amp;#039;.&amp;lt;ref&amp;gt;{{cite book |last1=O&amp;#039;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}}&amp;lt;/ref&amp;gt; However, the book describes using a &amp;#039;&amp;#039;&amp;#039;registry of singletons&amp;#039;&amp;#039;&amp;#039; to allow subclassing of singletons,&amp;lt;ref&amp;gt;{{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}}&amp;lt;/ref&amp;gt; which is essentially the multiton pattern.{{Citation needed|date=April 2012}}&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
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 [[wikt:null|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.&lt;br /&gt;
&lt;br /&gt;
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]].&lt;br /&gt;
&lt;br /&gt;
The multiton is unique in that it provides centralized access to a single directory (i.e. all keys are in the same namespace, &amp;#039;&amp;#039;per se&amp;#039;&amp;#039;) 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.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
This pattern, like the [[Singleton pattern]], makes [[unit testing]] far more difficult,&amp;lt;ref&amp;gt;{{Cite web | url=http://googletesting.blogspot.com/2008/11/clean-code-talks-global-state-and.html |title = Clean Code Talks - Global State and Singletons}}&amp;lt;/ref&amp;gt; as it introduces [[global variables|global state]] into an application.&lt;br /&gt;
&lt;br /&gt;
With garbage collected languages it may become a source of memory leaks as it introduces global strong references to the objects.&lt;br /&gt;
&lt;br /&gt;
==Implementations==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
In C#, we can also use enums, as the following example shows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c#&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
&lt;br /&gt;
public enum MultitonType&lt;br /&gt;
{&lt;br /&gt;
    Zero,&lt;br /&gt;
    One,&lt;br /&gt;
    Two&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Multiton&lt;br /&gt;
{&lt;br /&gt;
    private static readonly Dictionary&amp;lt;MultitonType, Multiton&amp;gt; instances =&lt;br /&gt;
        new Dictionary&amp;lt;MultitonType, Multiton&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    private MultitonType type;&lt;br /&gt;
&lt;br /&gt;
    private Multiton(MultitonType type)&lt;br /&gt;
    {&lt;br /&gt;
        this.type = type;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public static Multiton GetInstance(MultitonType type)&lt;br /&gt;
    {&lt;br /&gt;
        // Lazy init (not thread safe as written)&lt;br /&gt;
        // Recommend using Double Check Locking if needing thread safety&lt;br /&gt;
        if (!instances.TryGetValue(type, out var instance))&lt;br /&gt;
        {&lt;br /&gt;
            instance = new Multiton(type);&lt;br /&gt;
&lt;br /&gt;
            instances.Add(type, instance);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return instance;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public override string ToString()&lt;br /&gt;
    {&lt;br /&gt;
        return &amp;quot;My type is &amp;quot; + this.type;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Sample usage&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        var m0 = Multiton.GetInstance(MultitonType.Zero);&lt;br /&gt;
        var m1 = Multiton.GetInstance(MultitonType.One);&lt;br /&gt;
        var m2 = Multiton.GetInstance(MultitonType.Two);&lt;br /&gt;
&lt;br /&gt;
        Console.WriteLine(m0);&lt;br /&gt;
        Console.WriteLine(m1);&lt;br /&gt;
        Console.WriteLine(m2);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [https://rubygems.org/gems/multiton/versions/0.0.1 Multiton implementation in Ruby language]&lt;br /&gt;
* [https://github.com/PureMVC/puremvc-as3-multicore-framework/blob/master/src/org/puremvc/as3/multicore/patterns/facade/Facade.as Multiton usage in PureMVC Framework for ActionScript 3]&lt;br /&gt;
* [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]&lt;br /&gt;
&lt;br /&gt;
{{Design Patterns patterns}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Software design patterns]]&lt;br /&gt;
[[Category:Articles with example Java code]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Jlwoodwa</name></author>
	</entry>
</feed>