Click here to Skip to main content
15,893,486 members
Articles / Programming Languages / Visual Basic

Using an Observer design pattern to keep multiple forms up to date

Rate me:
Please Sign up or sign in to vote.
2.58/5 (8 votes)
29 May 20051 min read 55K   128   29  
This article demonstrates how multiple forms can be updated simultaneously using Singleton and Observer Design Patterns.
Public Class ColorObserver

    Public Shared Event ColorUpdated()
    Private Shared _SelectedColor As Colors
    Public Enum Colors
        Red = 0
        White = 1
        Blue = 2
    End Enum

#Region "Singleton Design Patern"
    REM Use a Singleton Design patern to ensure that this class can only be instanciated once
    Public Shared s As ColorObserver
    Public Shared flag As Boolean

    Private Sub New()
        'private constructor disallowing other to create object directly
    End Sub
    Public Shared Function getSingletonObject() As ColorObserver
        If flag = False Then
            'First instance
            'return a new object
            s = New ColorObserver
            flag = True
            Return s
        Else
            'future object request 
            'return an already created object
            Return s
        End If
    End Function
#End Region
#Region "Color Functions"
    Public Property Color() As Colors
        Get
            Return _SelectedColor
        End Get
        Set(ByVal Color As Colors)
            _SelectedColor = Color
            RaiseEvent ColorUpdated()
        End Set
    End Property
#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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
Software Developer (Senior)
Switzerland Switzerland
I am a Software Engineer currently working in a Re Insurance company.

Comments and Discussions