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:

PublicClass GlobalSettings

PrivateShared mySelf As GlobalSettings

PrivateSubNew()

EndSub

PublicSharedReadOnlyProperty GetInstance() As GlobalSettings

Get

If mySelf IsNothingThen

mySelf = New GlobalSettings

EndIf

Return mySelf

EndGet

EndProperty

EndClass

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.

PublicClass GlobalSettings

PrivateShared mySelf As GlobalSettings

PrivateSubNew()

EndSub

'I added a Property so you could see a use

PublicReadOnlyProperty CnString() AsString

Get

Return"My DB Connection String"

EndGet

EndProperty

PublicSharedReadOnlyProperty GetInstance() As GlobalSettings

Get

If mySelf IsNothingThen

SyncLock"Create GlobalSettings"

If mySelf IsNothingThen

mySelf = New GlobalSettings

EndIf

EndSyncLock

EndIf

Return mySelf

EndGet

EndProperty

EndClass

EndClass

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.

PublicClass MyCode

PublicFunction DBConnection() AsString

Dim dbCon1 AsString

Dim dbCon2 AsString

Dim gs As GlobalSettings = GlobalSettings.GetInstance

dbCon1 = gs.CnString

dbCon2 = GlobalSettings.GetInstance.CnString

Return dbCon2

EndFunction

EndClass

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!

You must have javascript enabled in order to post comments.

Leave a Comment

Your email address will not be published. All fields are required.

Name

Email

Title

Comment

Top White Papers and Webcasts

Are you truly leading your team or simply managing them? Organizations need leaders and your team needs someone to follow. With some ongoing development, you could become that leader. Learn the top leadership qualities that inspire others to want to follow you and the direction of your company.

The 2014 State of DevOps Report — based on a survey of 9,200+ people in IT operations, software development and technology management roles in 110 countries — reveals:
Companies with high-performing IT organizations are twice as likely to exceed their profitability, market share and productivity goals.
IT performance improves with DevOps maturity, and strongly correlates with well-known DevOps practices.
Job satisfaction is the No. 1 predictor of performance against organizational …