Home > DeveloperSection > Articles > Doubleton Design Pattern in C# – Part II

Doubleton Design Pattern in C# – Part II


ASP.Net ASP.Net 
Ratings:
1 Comment(s)
 2681  View(s)
Rate this:

Doubleton Design Pattern in C# – Part II

I would like to share a way by which we can create a class which could create maximum two objects.

This could be extension of Singleton pattern.

I would like to introduce following way to create doubleton class.

This Article could be extension of my last article.

Code Sample

sealed class doubleton2

    {

        private static readonly object doubletonLock = new object();

        private static int index = 0;

        private static List<doubleton2> list = new List<doubleton2>();

 

         private doubleton2() { }

         public static List<doubleton2> ReturnDoubletonList()

         {

        

             lock (doubletonLock)

             {

                 if (index < 2)

                 {

                     list.Add(new doubleton2());

                     index++;

                 }

 

             }

             return list;

 

         }

   

    }

   

    class Program

    {

        static void Main(string[] args)

        {

           List<doubleton2> lifirst = doubleton2.ReturnDoubletonList();

           List<doubleton2> lisecond= doubleton2.ReturnDoubletonList();

           List<doubleton2> lithird = doubleton2.ReturnDoubletonList();

           Console.ReadKey();

 

        }

    }

 

Understanding the code

a.       We created List object of  doubleton2 class

b.      On each call of ReturnDoubletonList() method we are just incrementing the value of this static field (index integer)

c.       ReturnDoubletonList () method is static and it returns  List object of Doubleton class

d.      We have index<2 inside ReturnDoubletonList () method to make sure we can have maximum two objects of doubleton class.

  1. After executing first line on main function

List<doubleton2> lifirst = doubleton2.ReturnDoubletonList();

We have a list of one object.

 

Doubleton Design Pattern in C# – Part II

  1. After executing second line on main function

List<doubleton2> liSecond = doubleton2.ReturnDoubletonList();

We have a list of two objects.

Doubleton Design Pattern in C# – Part II

 

  1. After executing third line on main function

List<doubleton2> lithird = doubleton2.ReturnDoubletonList();

Again we have a list of two objects because we have index<2 check in ReturndoubetonList function.

 

 

Doubleton Design Pattern in C# – Part II

 

Facts with the code

Here in above code following things could be note down.

  1. Object lifirst[0] is always equal to lisecond[0].

If I added following line to main function

Console.WriteLine("lifirst[0] and lisecond[0] are Equal: " + lifirst[0].Equals(lisecond[0]));

 

Then following would be output.

Doubleton Design Pattern in C# – Part II

 

Conclusion

From above way we can learn how to create doubleton class. Similarly we can N-ton class as well.

We can modify this class as per requirement.


Doubleton Design Pattern

By Mark Toy on   2 years ago
Well explained... Waiting for more knowledgeable articles from your side

Don't want to miss updates? Please click the below button!

Follow MindStick