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

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds