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

  • Live Event Date: May 6, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT Where are you in your plans to adopt Disaster Recovery-as-a-Service? Are you just getting started? Fighting an uphill battle with management? At Cisco, Zerto and iland, we've seen it all – from the early adopters who excitedly rushed to implement DRaaS with us nine years ago to the IT folks dragging their business leaders into the future. With our years of experience, we've learned there are six types of DRaaS leaders – but which type …

  • Live Event Date: May 18, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT While the idea of using facial and or gesture recognitions to create a modern, intuitive game seems attractive, some developers may want to leverage Unity 3D as a way to accelerate their development. There are many different ways in which Intel and Unity Technologies have been working together to helps speed the develop of games with the Intel® RealSense™ SDK (Software Developer Kit), so come hear from a panel of experts on what we've done …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date