TIP: The Singleton Pattern

Singletons are very nifty. They are objects that can only exist once in memory. I like to use them when I create programs that deal with threading. They are the perfect object to act as a a buffer to store the results of a bunch of threads that can also be accessed by the UI. You still need to think about thread safety but because only one copy of the object can exist inside your application all of the threads can use it to share information. You could also use it as a old fashioned "Global Variable" from your VB6 days, but personally I think that using it for that is bad software design. The main thing that makes something a singleton is making it's constructor private. How do you create an object with a private constructor? Well check out this code:

  Public Class GlobalSettings

    Private Shared mySelf As GlobalSettings

    Private Sub New()

    End Sub

    Public Shared ReadOnly Property GetInstance() As GlobalSettings

      Get

        If mySelf Is Nothing Then

          mySelf = New GlobalSettings

        End If

        Return mySelf

      End Get

    End Property

  End Class

TIP: The Singleton Pattern

I added a property so you could see something you could use this for, and I usually add a SyncLock to make the construction Thread Safe. The SyncLock command will only allow one tread into it at a time. I double up the IF statement to avoid hitting the SyncLock after the Object has been created.

  Public Class GlobalSettings

    Private Shared mySelf As GlobalSettings

    Private Sub New()

    End Sub

    'I added a Property so you could see a use

    Public ReadOnly Property CnString() As String

      Get

        Return "My DB Connection String"

      End Get

    End Property

    Public Shared ReadOnly Property GetInstance() As GlobalSettings

      Get

        If mySelf Is Nothing Then

          SyncLock "Create GlobalSettings"

            If mySelf Is Nothing Then

              mySelf = New GlobalSettings

            End If

          End SyncLock

        End If

        Return mySelf

      End Get

    End Property

  End Class

 

End Class



Now there are two easy ways to access this object in your code, I kinda like the second one better because you don't have to create another variable.

  Public Class MyCode

    Public Function DBConnection() As String

      Dim dbCon1 As String

      Dim dbCon2 As String

      Dim gs As GlobalSettings = GlobalSettings.GetInstance

      dbCon1 = gs.CnString

      dbCon2 = GlobalSettings.GetInstance.CnString

      Return dbCon2

    End Function

  End Class



About the Author

Daryl Butcher

I have been programming in Basic my entire life; I was 7 years old when I wrote my first computer program on a VIC20 in good old Basic. I continued to write in Basic and QBASIC until I was in college. At the age of 19, I starting writing software professionally. I have been professionally programming in Visual Basic for over 12 years. I was working at Microsoft as a VB6 developer when they released VB.NET and started working in .NET the same day it was released. At first I hated .NET, but I quickly learned to love it.

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

Most Popular Programming Stories

More for Developers

RSS Feeds