Multiton pattern: Difference between revisions

Content deleted Content added
sources for GoF, can't find a source that says "registry of singletons" is actually the same pattern though
Bender the Bot (talk | contribs)
m Drawbacks: HTTP to HTTPS for Blogspot
 
(4 intermediate revisions by 2 users not shown)
Line 3:
In [[software engineering]], the '''multiton pattern''' 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]].
 
Rather than having a single instance ''per application'' (e.g. the {{Javadoc:SE|package=java.lang|java/lang|Runtime}} object in the [[Java (programming language)|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 (book)|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}}
 
==Description==
Line 15:
 
==Drawbacks==
This pattern, like the [[Singleton pattern]], makes [[unit testing]] far more difficult,<ref>{{Cite web | url=httphttps://googletesting.blogspot.com/2008/11/clean-code-talks-global-state-and.html |title = Clean Code Talks - Global State and Singletons}}</ref> as it introduces [[global variables|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.
Line 63:
public override string ToString()
{
return $"My type is " + {this.type}";
}
 
// Sample usage
public static void Main(string[] args)
{
varMultiton m0 = Multiton.GetInstance(MultitonType.Zero);
varMultiton m1 = Multiton.GetInstance(MultitonType.One);
varMultiton m2 = Multiton.GetInstance(MultitonType.Two);
 
Console.WriteLine(m0);