Observer Design Pattern in Visual Basic .NET
Observer design pattern is one of the behavior design patterns. This pattern can be identified as publisher – subscriber model. This pattern defines one to many relationship between objects. When oneobjectsx change its states, all the defendent objects are notified automatically. That is why we name this as publisher-subscriber model.
There are main objects in this pattern called Subject and Observer . Observer is dependent object of the Subject and there can be any number of Observers. When subject changes its state all the Observers are notified automatically
Now lets look at the UML diagram of the observer design pattern
Subject
This interface has method for register and remove observers, and also notify the changes of the state of the object
Observer
Observer interface has the update() method where ConcreteObserver can implement it to receive the updates
ConcreteSubject
This class implement the interface Subject and maintain the list of the observers. When it receives update it can call notifyObservers()
to send updates to observers
ConcreteObserver
This concrete class implements the Observer interface and maintain a reference to ConcreteSubject
Observer Pattern Implementation in VB.NET
In the following example, I will show you how to implement the observer design pattern with Visual Basic code.
In the above diagram shows how Users are subscribed to Weather data feed. When there is a change in weather it will call the update method with the message and subscribers will receive the message
This is the code for Subject Interface
Public Interface Subject Sub Update(ByVal msg As String) Sub NotifyObservers() Sub AddObserver(ByVal o As Observer) Sub RemoveObserver(ByVal o As Observer) End Interface
This is the code for Observer Interface
Public Interface Observer Sub Update(ByVal msg As String) End Interface
This is the code for Users Concrete Class
Public Class Users Implements Observer Dim m_subject As Subject Dim m_name As String Sub New(ByVal obj As Subject, ByVal name As String) Me.m_subject = obj obj.AddObserver(Me) Me.m_name = name End Sub Public Sub Update(ByVal msg As String) Implements Observer.Update Console.WriteLine(m_name + " Received Updates " + msg) End Sub End Class
This is the code for WeatherFeed Concrete Class
Public Class WeatherDataFeed Implements Subject Dim observers As New List(Of Observer) Dim msg As String Public Sub AddObserver(o As Observer) Implements Subject.AddObserver observers.Add(o) End Sub Public Sub NotifyObservers() Implements Subject.NotifyObservers For Each obj As Observer In observers obj.Update(Me.msg) Next End Sub Public Sub RemoveObserver(o As Observer) Implements Subject.RemoveObserver observers.Remove(o) End Sub Public Sub Update(ByVal msg As String) Implements Subject.Update Me.msg = msg NotifyObservers() End Sub End Class
You can write following code to execute the above observer pattern
Module Module1 Sub Main() Dim w_feed As Subject = New WeatherDataFeed() Dim m_observer As Observer = New Users(w_feed, "John") Dim ob2 As Observer = New Users(w_feed, "Taylor") w_feed.Update("Snow start at 10.30 AM") w_feed.RemoveObserver(ob2) w_feed.Update("Raining start at 11.30 AM") Console.ReadLine() End Sub End Module
Now you can see the following output
John Received Updates Snow start at 10.30 AM Taylor Received Updates Snow start at 10.30 AM John Received Updates Raining start at 11.30 AM