Multiton pattern: Difference between revisions

Content deleted Content added
Correction of C# example
Line 87:
{
return _instances.GetOrAdd(key, (k) => new T());
}
}
}
</source>
A fairly simple example of the multiton pattern, with a private constructor, and a tracker for created objects, the name being an integer identifier.
<source lang=CSharp>
//IVSR: Multiton design pattern
namespace IVSR.DesignPatterns.Multiton
{
public class Multiton
{
//read-only dictionary to track multitons
private static IDictionary<int, Multiton> _Tracker = new Dictionary<int, Multiton> { };
 
private Multiton()
{
}
 
public static Multiton GetInstance(int key)
{
//value to return
Multiton item = null;
//lock collection to prevent changes during operation
lock (_Tracker)
{
//if value not found, create and add
if(!_Tracker.TryGetValue(key, out item))
{
item = new Multiton();
 
//calculate next key
int newIdent = _Tracker.Keys.Max() + 1;
 
//add item
_Tracker.Add(newIdent, item);
}
}
return item;
}
}