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


This interface has method for register and remove observers, and also notify the changes of the state of the object


Observer interface has the update() method where ConcreteObserver can implement it to receive the updates


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


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

This is the code for Observer Interface

This is the code for Users Concrete Class

This is the code for WeatherFeed Concrete Class

You can write following code to execute the above observer pattern

Now you can see the following output