Tuesday, March 9, 2010

Singleton Pattern!

Singleton Pattern... A right choice to start our journey of learning design patterns with, because it's not that hard to understand and we should take start with easy things before moving forward to the more difficult and tricky ones.

Let's first examine what official definition says:

"Ensure a class has only one instance and provide a global point of access to it."

pretty straight forward? although many of you may find it hard to get a concept from a definition like myself, so we'll practically implement it.

But let's first elaborate the definition or should i say break it into parts to understand it more deeply, shall we? ok Great!

The definition can be broken into parts like:

  1. Ensure a class has only one instance
  2. Provide a global point of access to it

The very first point suggest us to design a class (I am supposing here you already know what a class is!) in such a way that it can be instantiated only once. You can think of the object instantiated as ONE OF ITS KIND, throughout your application not another instance of the same object can be created. Let's have a look at the example:

Class ScoreBoard

..........Private Shared _instance As ScoreBoard

..........Private Sub New()
....................'private constructor
..........End Sub

..........Public Shared Function GetInstance() As ScoreBoard

....................If _instance Is Nothing Then

.............................._instance = New ScoreBoard()

....................End If

....................Return _instance

..........End Function

End Class

Let's examine the class, the very first thing you might have noticed is it's constructor which is private which means you cannot directly instantiate the class, you have to call it's GetInstance() function to instantiate the object, which brings us on the Function itself where we've assured that if the instance is already there we don't need to create another remember the 1st point? Ensure a class has only one instance. Now let's use the class:

Sub Main()

..........Dim s1 = ScoreBoard.GetInstance()
..........Dim s2 = ScoreBoard.GetInstance()

..........If s1.Equals(s2) Then

....................Console.WriteLine("Objects are same...")

..........End If

..........'Just to hold the window until user presses a key
..........Console.ReadLine()

End Sub

Output:

Objects are same...

ohh... what about the second point? Provide a global point of access to it... Relax! it's there in the Main function ScoreBoard.GetInstance()!

Making it safe!

what if you are working in a multi-threaded application and threads are trying to access the GetInstance() function at the same time? You can use any locking mechanism provided by .NET e.g.

Class ScoreBoard

..........Private Shared _instance As ScoreBoard
..........Private Shared synchLock As New Object()

..........Private Sub New()
....................'private constructor
..........End Sub

..........Public Shared Function GetInstance() As ScoreBoard

....................SyncLock synchLock

..............................If _instance Is Nothing Then

........................................_instance = New ScoreBoard()

..............................End If

....................End SyncLock

....................Return _instance

..........End Function

End Class

Thanks for your patience and I hope you got the concept of this pattern. So, stay live for more!

Monday, March 8, 2010

Head first design pattern a must have!

Head first design pattern is a great book to start your learning with on Design patterns. Because the way they present a particular pattern is what makes it a unique book and a must have for every developer, although the code examples is written in Java but anyone working in any language what so ever can have benefit from it. I'll be posting my learning on each design pattern from the book but they are not in any case an alternate to the material presented in the book because It's much more elaborated in the book then the ones I'll be presenting here.