Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Starting from the Singleton Design Pattern to Exploring 'The Doubleton Design Pattern' to N-ton...

0.00/5 (No votes)
9 May 2005 1  
An article which describes the Doubleton design pattern.

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

/// <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 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:

    /// <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 Doubleton object and set its DataValue property to 1:

    // 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 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here