Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / Visual Basic
Article

Design Patterns using VB.NET

Rate me:
Please Sign up or sign in to vote.
2.07/5 (30 votes)
30 Sep 20021 min read 107.5K   24   6
Creating Desing Patterns using VB.NET

Patterns are devices that allow programs to share knowledge about their design. In this article we will try to write implement Singleton patterns using VB.NET. The Singleton pattern applies to many situation where there needs to be only a single instance of the class.

How do we make sure that the class can have only one instance ? One solution is to use a global variable, but it wont stop the client from making multiple instances. The better solutoin would be to make the class intelligent enough to restrict clients from creating more than one instance. This is the Singleton Pattern.

Below is the implementation of a singleton class using VB.NET
Public Class clsSingleton

    Private Shared objSingle As clsSingleton
    Private Shared blCreated As Boolean
    Public strI As String

    Private Sub New()
        'Override the default constructor
    End Sub

    Public Shared Function getObject() As clsSingleton
        If blCreated = False Then
            objSingle = New clsSingleton()
            blCreated = True
            Return objSingle
        Else
            Return objSingle
        End If
    End Function
End Class

Let us now examine the above class. In the above class we have made the construtor as private, so that the client cannot create instance of this class using new operator. Function getObject is called to get an instance of the class. Based on the boolean variable blCreated, only during the first time an instance is created. During subsequent calls to getObject no new instance are created but the older one is returned, thus ensuring only one instance of the class is created.

You can test this class by writing clients which can create multiple instance of the above class. By setting values to the string variable in the class you will discover that the class instance is created only once. Now that we have implementation, where can we use this ? There are many instances where microsoft have effectively used singleton pattern like, there exist only one FileSystem in windows,only one window manager etc.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMicrosoft's Recommendation Pin
Kevin McFarlane1-Oct-02 1:37
Kevin McFarlane1-Oct-02 1:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.