Introduction
This article describes how the 'Doubleton' design pattern works, and how it can be extended to multiple instances.
Background
Many 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 code
For the purpose of this article, consider a class called Doubleton
. The code for the class Doubleton
is shown below...
The Doubleton class
public class Doubleton
{
#region Private variables
private static int instanceCount = -1;
private static bool switchOver = false;
private int _data = 0;
private static ArrayList _list = null;
#endregion
static Doubleton()
{
_list = new ArrayList();
}
private Doubleton()
{
}
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;
}
}
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 instanceCount
is used to keep track of the number of instances of an object. The _data
is used to hold a data value, this can be used for any other purpose, as required by the business logic. The _list
is of type ArrayList
which is used to hold the instances of the object created.
The static
constructor static Doubleton()
is called before any other instance constructor is called in the code. This is used to initialize the ArrayList
declared using the variable _list
.
The default constructor private Doubleton()
is set to private access, so as to prevent external callers from creating an instance using the new
operator.
The property/method public static Doubleton Instance
is used to return an instance of this Doubleton
object. This method first checks the value of instanceCount
and checks to see if its value is greater than or equal to 1, if yes, it returns the first item in the _list
object. Initially the value of instanceCount
is -1, and hence it adds the Doubleton
object to the ArrayList
.
The source code below explains the usage of the Doubleton
object:
class DoubletonTest
{
[STAThread]
static void Main(string[] args)
{
Doubleton dx1 = Doubleton.Instance;
dx1.DataValue = 1;
Doubleton dx2 = Doubleton.Instance;
dx2.DataValue = 2;
Doubleton dx3 = Doubleton.Instance;
dx3.DataValue = 123;
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 Doubleton
object and set its DataValue
property to 1:
Doubleton dx1 = Doubleton.Instance;
dx1.DataValue = 1;
Consider the code snippet below...
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 Doubleton
object and set its DataValue
property to 2. The doubleton object's instance dx3
's, DataValue
property is now set to 123. Since the instances dx1
and dx3
refer to the same instance, both of them would have the same DataValue
, 123.
Points of Interest - A Tripleton, ..., N-ton.
- This pattern with a little modification can be used to create controlled instances of several objects.
- A
Tripleton
class for instance, would be creating only three instances of an object.
- An N-ton can be used to create controlled N-instances of a class.