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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • QA teams don't have time to test everything yet they can't afford to ship buggy code. Learn how Coverity can help organizations shrink their testing cycles and reduce regression risk by focusing their manual and automated testing based on the impact of change.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds