|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article describes how the 'Doubleton' design pattern works, and how it can be extended to multiple instances. BackgroundMany articles have been written about the Singleton design pattern in the history of Computer Science. I assume that the readers of this article have the knowledge of Singleton design pattern. Briefly, a Singleton design pattern is used to maintain only one instance of an object at any point of time. So whenever a request is made to retrieve an object only the same instance will be returned. In the Doubleton design pattern, continuing on the same lines, there will be two instances of an object at any point of time. The example described below can be modified to create N-multiple instances of any desired object. Using the codeFor the purpose of this article, consider a class called The Doubleton class/// <SUMMARY>
/// A Doubleton - Maintains two instances of
/// an object at any point in time.
/// </SUMMARY>
public class Doubleton
{
#region Private variables
/// <SUMMARY>
/// This variable maintains the instance count.
/// </SUMMARY>
private static int instanceCount = -1;
/// <SUMMARY>
/// This flag is used to return alternate instances.
/// </SUMMARY>
private static bool switchOver = false;
/// <SUMMARY>
/// This variable holds the data value.
/// </SUMMARY>
private int _data = 0;
/// <SUMMARY>
/// Holds the instance of the arraylist.
/// </SUMMARY>
private static ArrayList _list = null;
#endregion
/// <SUMMARY>
/// Static constructor
/// </SUMMARY>
static Doubleton()
{
_list = new ArrayList();
}
/// <SUMMARY>
/// Private constructor
/// </SUMMARY>
private Doubleton()
{
}
/// <SUMMARY>
/// Factory method to retrieve the
/// instance of the object.
/// </SUMMARY>
public static Doubleton Instance
{
get
{
if (switchOver)
{
switchOver = false;
return _list[1] as Doubleton;
}
if( instanceCount >= 1)
{
switchOver = true;
return _list[0] as Doubleton;
}
instanceCount++;
_list.Add( new Doubleton() );
return _list[instanceCount] as Doubleton;
}
}
/// <SUMMARY>
/// Holds the data value of the object.
/// </SUMMARY>
public int DataValue
{
get
{
return _data;
}
set
{
_data = value;
}
}
}
As you can see in the code above, there are three member variables declared. The first one The The default constructor The property/method The source code below explains the usage of the /// <SUMMARY>
/// A client application to test the 'Doubleton' object.
/// </SUMMARY>
class DoubletonTest
{
[STAThread]
static void Main(string[] args)
{
// This would get the first instance.
Doubleton dx1 = Doubleton.Instance;
dx1.DataValue = 1;
// This would get the second instance.
Doubleton dx2 = Doubleton.Instance;
dx2.DataValue = 2;
// This would again get the first instance.
Doubleton dx3 = Doubleton.Instance;
dx3.DataValue = 123;
// This would again get the second instance.
Doubleton dx4 = Doubleton.Instance;
Console.WriteLine("DataValue dx1 = " + dx1.DataValue);
Console.WriteLine("DataValue dx2 = " + dx2.DataValue);
Console.WriteLine("DataValue dx3 = " + dx3.DataValue);
Console.WriteLine("DataValue dx4 = " + dx4.DataValue);
Console.ReadLine();
}
}
The source code snippet below would create an instance of a // This would get the first instance.
Doubleton dx1 = Doubleton.Instance;
dx1.DataValue = 1;
Consider the code snippet below... // This would get the second instance.
Doubleton dx2 = Doubleton.Instance;
dx2.DataValue = 2;
Doubleton dx3 = Doubleton.Instance;
dx3.DataValue = 123;
Doubleton dx4 = Doubleton.Instance;
The source code snippet would create another instance of a Points of Interest - A Tripleton, ..., N-ton.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||