Observer Design Pattern

The observer design pattern enables a subscriber to register with and receive notifications from a provider. It is suitable for any scenario that requires push-based notification. The pattern defines a provider (also known as a subject or an observable) and zero, one, or more observers. Observers register with the provider, and whenever a predefined condition, event, or state change occurs, the provider automatically notifies all observers by calling one of their methods. In this method call, the provider can also provide current state information to observers. In the .NET Framework, the observer design pattern is applied by implementing the generic System.IObservable(Of T) and System.IObserver(Of T) interfaces. The generic type parameter represents the type that provides notification information.

The observer design pattern is suitable for distributed push-based notifications, because it supports a clean separation between two different components or application layers, such as a data source (business logic) layer and a user interface (display) layer. The pattern can be implemented whenever a provider uses callbacks to supply its clients with current information.

Implementing the pattern requires that you provide the following:

  • A provider or subject, which is the object that sends notifications to observers. A provider is a class or structure that implements the IObservable(Of T) interface. The provider must implement a single method, IObservable(Of T).Subscribe, which is called by observers that wish to receive notifications from the provider.

  • An observer, which is an object that receives notifications from a provider. An observer is a class or structure that implements the IObserver(Of T) interface. The observer must implement three methods, all of which are called by the provider:

  • A mechanism that allows the provider to keep track of observers. Typically, the provider uses a container object, such as a System.Collections.Generic.List(Of T) object, to hold references to the IObserver(Of T) implementations that have subscribed to notifications. Using a storage container for this purpose enables the provider to handle zero to an unlimited number of observers. The order in which observers receive notifications is not defined; the provider is free to use any method to determine the order.

  • An IDisposable implementation that enables the provider to remove observers when notification is complete. Observers receive a reference to the IDisposable implementation from the Subscribe method, so they can also call the IDisposable.Dispose method to unsubscribe before the provider has finished sending notifications.

  • An object that contains the data that the provider sends to its observers. The type of this object corresponds to the generic type parameter of the IObservable(Of T) and IObserver(Of T) interfaces. Although this object can be the same as the IObservable(Of T) implementation, most commonly it is a separate type.

Note Note

In addition to implementing the observer design pattern, you may be interested in exploring libraries that are built using the IObservable(Of T) and IObserver(Of T) interfaces. For example, Reactive Extensions for .NET (Rx) consist of a set of extension methods and LINQ standard sequence operators to support asynchronous programming.

The following example uses the observer design pattern to implement an airport baggage claim information system. A BaggageInfo class provides information about arriving flights and the carousels where baggage from each flight is available for pickup. It is shown in the following example.

Public Class BaggageInfo
   Private flightNo As Integer 
   Private origin As String 
   Private location As Integer 

   Friend Sub New(ByVal flight As Integer, ByVal from As String, ByVal carousel As Integer)
      Me.flightNo = flight
      Me.origin = from
      Me.location = carousel
   End Sub 

   Public ReadOnly Property FlightNumber As Integer 
      Get 
         Return Me.flightNo
      End Get 
   End Property 

   Public ReadOnly Property From As String 
      Get 
         Return Me.origin
      End Get 
   End Property 

   Public ReadOnly Property Carousel As Integer 
      Get 
         Return Me.location
      End Get 
   End Property 
End Class

A BaggageHandler class is responsible for receiving information about arriving flights and baggage claim carousels. Internally, it maintains two collections:

  • observers - A collection of clients that will receive updated information.

  • flights - A collection of flights and their assigned carousels.

Both collections are represented by generic List(Of T) objects that are instantiated in the BaggageHandler class constructor. The source code for the BaggageHandler class is shown in the following example.

Public Class BaggageHandler : Implements IObservable(Of BaggageInfo)

   Private observers As List(Of IObserver(Of BaggageInfo))
   Private flights As List(Of BaggageInfo)

   Public Sub New()
      observers = New List(Of IObserver(Of BaggageInfo))
      flights = New List(Of BaggageInfo)
   End Sub 

   Public Function Subscribe(ByVal observer As IObserver(Of BaggageInfo)) As IDisposable _
                   Implements IObservable(Of BaggageInfo).Subscribe
      ' Check whether observer is already registered. If not, add it 
      If Not observers.Contains(observer) Then
         observers.Add(observer)
         ' Provide observer with existing data. 
         For Each item In flights
            observer.OnNext(item)
         Next 
      End If 
      Return New Unsubscriber(Of BaggageInfo)(observers, observer)
   End Function 

   ' Called to indicate all baggage is now unloaded. 
   Public Sub BaggageStatus(ByVal flightNo As Integer)
      BaggageStatus(flightNo, String.Empty, 0)
   End Sub 

   Public Sub BaggageStatus(ByVal flightNo As Integer, ByVal from As String, ByVal carousel As Integer)
      Dim info As New BaggageInfo(flightNo, from, carousel)

      ' Carousel is assigned, so add new info object to list. 
      If carousel > 0 And Not flights.Contains(info) Then
         flights.Add(info)
         For Each observer In observers
            observer.OnNext(info)
         Next 
      ElseIf carousel = 0 Then 
         ' Baggage claim for flight is done 
         Dim flightsToRemove As New List(Of BaggageInfo)
         For Each flight In flights
            If info.FlightNumber = flight.FlightNumber Then
               flightsToRemove.Add(flight)
               For Each observer In observers
                  observer.OnNext(info)
               Next 
            End If 
         Next 
         For Each flightToRemove In flightsToRemove
            flights.Remove(flightToRemove)
         Next
         flightsToRemove.Clear()
      End If 
   End Sub 

   Public Sub LastBaggageClaimed()
      For Each observer In observers
         observer.OnCompleted()
      Next
      observers.Clear()
   End Sub 
End Class

Clients that wish to receive updated information call the BaggageInfo.Subscribe method. If the client has not previously subscribed to notifications, a reference to the client's IObserver(Of T) implementation is added to the observers collection.

The overloaded BaggageHandler.BaggageStatus method can be called to indicate that baggage from a flight either is being unloaded or is no longer being unloaded. In the first case, the method is passed a flight number, the airport from which the flight originated, and the carousel where baggage is being unloaded. In the second case, the method is passed only a flight number. For baggage that is being unloaded, the method checks whether the BaggageInfo information passed to the method exists in the flights collection. If it does not, the method adds the information and calls each observer's OnNext method. For flights whose baggage is no longer being unloaded, the method checks whether information on that flight is stored in the flights collection. If it is, the method calls each observer's OnNext method and removes the BaggageInfo object from the flights collection.

When the last flight of the day has landed and its baggage has been processed, the BaggageHandler.LastBaggageClaimed method is called. This method calls each observer's OnCompleted method to indicate that all notifications have completed, and then clears the observers collection.

The provider's Subscribe method returns an IDisposable implementation that enables observers to stop receiving notifications before the OnCompleted method is called. The source code for this Unsubscriber(Of BaggageInfo) class is shown in the following example. When the class is instantiated in the BaggageHandler.Subscribe method, it is passed a reference to the observers collection and a reference to the observer that is added to the collection. These references are assigned to local variables. When the object's Dispose method is called, it checks whether the observer still exists in the observers collection, and, if it does, removes the observer.

Friend Class Unsubscriber(Of BaggageInfo) : Implements IDisposable
   Private _observers As List(Of IObserver(Of BaggageInfo))
   Private _observer As IObserver(Of BaggageInfo)

   Friend Sub New(ByVal observers As List(Of IObserver(Of BaggageInfo)), ByVal observer As IObserver(Of BaggageInfo))
      Me._observers = observers
      Me._observer = observer
   End Sub 

   Public Sub Dispose() Implements IDisposable.Dispose
      If _observers.Contains(_observer) Then
         _observers.Remove(_observer)
      End If 
   End Sub 
End Class

The following example provides an IObserver(Of T) implementation named ArrivalsMonitor, which is a base class that displays baggage claim information. The information is displayed alphabetically, by the name of the originating city. The methods of ArrivalsMonitor are marked as overridable (in Visual Basic) or virtual (in C#), so they can all be overridden by a derived class.

Public Class ArrivalsMonitor : Implements IObserver(Of BaggageInfo)
   Private name As String 
   Private flightInfos As New List(Of String)
   Private cancellation As IDisposable
   Private fmt As String = "{0,-20} {1,5}  {2, 3}" 

   Public Sub New(ByVal name As String)
      If String.IsNullOrEmpty(name) Then Throw New ArgumentNullException("The observer must be assigned a name.")

      Me.name = name
   End Sub 

   Public Overridable Sub Subscribe(ByVal provider As BaggageHandler)
      cancellation = provider.Subscribe(Me)
   End Sub 

   Public Overridable Sub Unsubscribe()
      cancellation.Dispose()
      flightInfos.Clear()
   End Sub 

   Public Overridable Sub OnCompleted() Implements System.IObserver(Of BaggageInfo).OnCompleted
      flightInfos.Clear()
   End Sub 

   ' No implementation needed: Method is not called by the BaggageHandler class. 
   Public Overridable Sub OnError(ByVal e As System.Exception) Implements System.IObserver(Of BaggageInfo).OnError
      ' No implementation. 
   End Sub 

   ' Update information. 
   Public Overridable Sub OnNext(ByVal info As BaggageInfo) Implements System.IObserver(Of BaggageInfo).OnNext
      Dim updated As Boolean = False 

      ' Flight has unloaded its baggage; remove from the monitor. 
      If info.Carousel = 0 Then 
         Dim flightsToRemove As New List(Of String)
         Dim flightNo As String = String.Format("{0,5}", info.FlightNumber)
         For Each flightInfo In flightInfos
            If flightInfo.Substring(21, 5).Equals(flightNo) Then
               flightsToRemove.Add(flightInfo)
               updated = True 
            End If 
         Next 
         For Each flightToRemove In flightsToRemove
            flightInfos.Remove(flightToRemove)
         Next
         flightsToRemove.Clear()
      Else 
         ' Add flight if it does not exist in the collection. 
         Dim flightInfo As String = String.Format(fmt, info.From, info.FlightNumber, info.Carousel)
         If Not flightInfos.Contains(flightInfo) Then
            flightInfos.Add(flightInfo)
            updated = True 
         End If 
      End If 
      If updated Then
         flightInfos.Sort()
         Console.WriteLine("Arrivals information from {0}", Me.name)
         For Each flightInfo In flightInfos
            Console.WriteLine(flightInfo)
         Next
         Console.WriteLine()
      End If 
   End Sub 
End Class

The ArrivalsMonitor class includes the Subscribe and Unsubscribe methods. The Subscribe method enables the class to save the IDisposable implementation returned by the call to Subscribe to a private variable. The Unsubscribe method enables the class to unsubscribe from notifications by calling the provider's Dispose implementation. ArrivalsMonitor also provides implementations of the OnNext, OnError, and OnCompleted methods. Only the OnNext implementation contains a significant amount of code. The method works with a private, sorted, generic List(Of T) object that maintains information about the airports of origin for arriving flights and the carousels on which their baggage is available. If the BaggageHandler class reports a new flight arrival, the OnNext method implementation adds information about that flight to the list. If the BaggageHandler class reports that the flight's baggage has been unloaded, the OnNext method removes that flight from the list. Whenever a change is made, the list is sorted and displayed to the console.

The following example contains the application entry point that instantiates the BaggageHandler class as well as two instances of the ArrivalsMonitor class, and uses the BaggageHandler.BaggageStatus method to add and remove information about arriving flights. In each case, the observers receive updates and correctly display baggage claim information.

Module Example
   Public Sub Main()
      Dim provider As New BaggageHandler()
      Dim observer1 As New ArrivalsMonitor("BaggageClaimMonitor1")
      Dim observer2 As New ArrivalsMonitor("SecurityExit")

      provider.BaggageStatus(712, "Detroit", 3)
      observer1.Subscribe(provider)
      provider.BaggageStatus(712, "Kalamazoo", 3)
      provider.BaggageStatus(400, "New York-Kennedy", 1)
      provider.BaggageStatus(712, "Detroit", 3)
      observer2.Subscribe(provider)
      provider.BaggageStatus(511, "San Francisco", 2)
      provider.BaggageStatus(712)
      observer2.Unsubscribe()
      provider.BaggageStatus(400)
      provider.LastBaggageClaimed()
   End Sub 
End Module 
' The example displays the following output: 
'      Arrivals information from BaggageClaimMonitor1 
'      Detroit                712    3 

'      Arrivals information from BaggageClaimMonitor1 
'      Detroit                712    3 
'      Kalamazoo              712    3 

'      Arrivals information from BaggageClaimMonitor1 
'      Detroit                712    3 
'      Kalamazoo              712    3 
'      New York-Kennedy       400    1 

'      Arrivals information from SecurityExit 
'      Detroit                712    3 

'      Arrivals information from SecurityExit 
'      Detroit                712    3 
'      Kalamazoo              712    3 

'      Arrivals information from SecurityExit 
'      Detroit                712    3 
'      Kalamazoo              712    3 
'      New York-Kennedy       400    1 

'      Arrivals information from BaggageClaimMonitor1 
'      Detroit                712    3 
'      Kalamazoo              712    3 
'      New York-Kennedy       400    1 
'      San Francisco          511    2 

'      Arrivals information from SecurityExit 
'      Detroit                712    3 
'      Kalamazoo              712    3 
'      New York-Kennedy       400    1 
'      San Francisco          511    2 

'      Arrivals information from BaggageClaimMonitor1 
'      New York-Kennedy       400    1 
'      San Francisco          511    2 

'      Arrivals information from SecurityExit 
'      New York-Kennedy       400    1 
'      San Francisco          511    2 

'      Arrivals information from BaggageClaimMonitor1 
'      San Francisco          511    2

Title

Description

Observer Design Pattern Best Practices

Describes best practices to adopt when developing applications that implement the observer design pattern.

How to: Implement a Provider

Provides a step-by-step implementation of a provider for a temperature monitoring application.

How to: Implement an Observer

Provides a step-by-step implementation of an observer for a temperature monitoring application.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft