Let's first examine what official definition says:
"Ensure a class has only one instance and provide a global point of access to it."
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: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:
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
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:
- Ensure a class has only one instance
- 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
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!