Click here to Skip to main content
13,665,664 members
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 29 May 2005

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

, 29 May 2005
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how multiple forms can be updated simultaneously using Singleton and Observer Design Patterns.


This article describes how to keep multiple instances of objects up to date using Singleton and Observer Design Patterns.

Singleton Design Pattern

We want to be able to share a variable between different objects. The first step to doing this is to ensure that we store the variable only once. To do this we use a Singleton class, this is a class that can only be instantiated once.

The code below is a VB.NET translation of a C# version of a Singleton that can be downloaded from

Public Class ColorObserver

#Region "Singleton Design Pattern"
    REM Use a Singleton Design pattern to ensure _
            that this class can only be instantiated 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
            'future object request 
            'return an already created object
            Return s
        End If
    End Function
#End Region
End Class

Observer Design Pattern

The next step is to be notified when a change occurs. This can be done using an Observer design pattern. There are many ways of implementing an Observer pattern. An Observer implemented with inheritance can be downloaded from here. I have chosen different approaches by using Delegates and Events as described in MSDN. The idea is to raise an event when data changes. Objects subscribe to the observer by listening for the ColorUpdated event.

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"
#End Region

#Region "Color Functions"
    Public Property Color() As Colors
            Return _SelectedColor
        End Get
        Set(ByVal Color As Colors)
            _SelectedColor = Color
            RaiseEvent ColorUpdated()
        End Set
    End Property
#End Region

End Class

To subscribe to this observer, you need:

Private iColorObserver As ColorObserver

Private Sub frmChild_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
    iColorObserver = iColorObserver.getSingletonObject
    AddHandler iColorObserver.ColorUpdated, AddressOf ChangeColor
End Sub

Private Sub ChangeColor()
    Select Case iColorObserver.Color
        Case ColorObserver.Colors.Red
            Me.BackColor = Color.Red
        Case ColorObserver.Colors.White
            Me.BackColor = Color.White
        Case ColorObserver.Colors.Blue
            Me.BackColor = Color.Blue
    End Select
End Sub

To update the color of all objects subscribing to the Observer:

iColorObserver.Color = ColorObserver.Colors.Red


This is a good way of propagating updates across multiple windows. I hope you enjoyed the example.


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


About the Author

Software Developer (Senior)
Switzerland Switzerland
I am a Software Engineer currently working in a Re Insurance company.

You may also be interested in...


Comments and Discussions

GeneralThread Safety Pin
Nigel-Findlater31-May-05 19:47
memberNigel-Findlater31-May-05 19:47 
GeneralHere are some simplifications Pin
Nigel-Findlater31-May-05 19:30
memberNigel-Findlater31-May-05 19:30 
GeneralRe: Here are some simplifications Pin
fiwi463pfa30-Oct-07 0:09
memberfiwi463pfa30-Oct-07 0:09 
GeneralMemory leak Pin
wout de zeeuw30-May-05 0:18
memberwout de zeeuw30-May-05 0:18 
GeneralRe: Memory leak Pin
Trance Junkie30-May-05 1:16
memberTrance Junkie30-May-05 1:16 
GeneralRe: Memory leak Pin
wout de zeeuw30-May-05 8:32
memberwout de zeeuw30-May-05 8:32 
GeneralRe: Memory leak Pin
ddarko10016-Oct-09 5:17
memberddarko10016-Oct-09 5:17 
GeneralRe: Memory leak Pin
Nigel-Findlater31-May-05 19:49
memberNigel-Findlater31-May-05 19:49 
GeneralRe: Memory leak Pin
wout de zeeuw31-May-05 22:35
memberwout de zeeuw31-May-05 22:35 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180810.1 | Last Updated 30 May 2005
Article Copyright 2005 by Nigel-Findlater
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid