Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns for Dummies. The Singleton Pattern

Posted in Design Patterns
This post has been read 3695 times

In continuation of my posts on Creational design patterns, I will be discussing about the Singleton pattern in this post. You can read about the other patterns from the following links.

  1. Prototype Pattern
  2. Factory Method Pattern

You can read about Structural Patterns from here.

Singleton pattern deals with ensuring that there is only one instance of a class and providing a single global access point to that object. It also ensures that the object is not created until its actually needed.

    1 class SingletonPattern

    2 {

    3     class SingletonPerson

    4     {

    5         private static SingletonPerson instance = null;

    6 

    7         public string Name { get; private set; }

    8         public int Age { get; private set; }

    9         public string Address { get; private set; }

   10 

   11         private Random randomnew Random();

   12 

   13         SingletonPerson()

   14         {

   15             Name = "Person" + random.Next( 1, 10 );

   16             Age = 27;

   17             Address = "Address" + random.Next(10, 20);

   18         }

   19 

   20         public static SingletonPerson Instance

   21         {

   22             get

   23             {

   24                 return Nested.Instance;

   25             }

   26         }

   27 

   28         private class Nested

   29         {

   30             internal static readonly SingletonPerson Instance =

   31                 new SingletonPerson();

   32 

   33             static Nested()

   34             {               

   35             }

   36         }

   37     }

   38 

   39     static void Main()

   40     {

   41         SingletonPerson person1 = SingletonPerson.Instance;

   42         Console.WriteLine( "Name  : " + person1.Name + " Age : " + person1.Age

   43             + " Address : " + person1.Address );

   44         //Output : Name  : Person7 Age : 27 Address : Address19

   45 

   46         SingletonPerson person2 = SingletonPerson.Instance;

   47         Console.WriteLine( "Name  : " + person2.Name + " Age : " + person2.Age

   48             + " Address : " + person2.Address );

   49         //Output : Name  : Person7 Age : 27 Address : Address19

   50 

   51         // Confirm person1 and person2 are same

   52         if ( person1 == person2 )

   53         {

   54             Console.WriteLine( "Same instance\n" );

   55         }

   56 

   57         Console.ReadLine();

   58     }

   59 }

As you can see from the above example the 2 instances of the SingletonPerson object are the same and so are their properties viz. Name, Age and Address.  We can use the singleton pattern when we need to create only one instance of a class or when we need to provide controlled access to an instance.

In my next post I will be writing about the Abstract Factory Pattern.




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll