Creating a Thread Safe Singleton in VB.NET
The singleton pattern is one of the best-known patterns in software engineering. Essentially, a singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance.
The first thing we want to do is declare a couple of variables:
Private Shared instance As ApplicationDAC Private Shared objectLock As Object = New ObjectThis first variable is of the type of the class that we want to create the singleton for. In this case it is the fictional class "ApplicationDAC". The second is an object that we will use for locking when callers attempt to access this instance of the "ApplicationDAC". Note that both these variables are static, or in VB.NET parlance, "Shared".
It's usually good practice to make the default class constructor private, so class consumers don't inadvertadly willy-nilly create instances of our object.
Private Sub New() End Sub
back
|
next
|
about