Introduction
The Observer design pattern falls under the category of "Behavioral Pattern". It is also called "Publish-Subscribe" pattern. This design pattern describes a one-to-many relationship between Observable (Subject) and Observer. It means when subject changes , all Observers are notified automatically.
E.g. A company has so many employees and employees mobile number, email id are registered in the portal of company so that company can communicate with all employees at the same time. Company informs all employees immediately, when events like urgent holiday, client visit, etc. occurs. The employees can receive information in different ways like sms, email, etc.
I will explain this "Observer Design" pattern with the implementation of "Event-Driven Approach". I will not explain it in the mode of object oriented approach. So, it will be little different.
The following are the two basic objects which are involved in the Observer Design pattern:
The Observer design pattern falls under the category of "Behavioral Pattern". It is also called "Publish-Subscribe" pattern. This design pattern describes a one-to-many relationship between Observable (Subject) and Observer. It means when subject changes , all Observers are notified automatically.
E.g. A company has so many employees and employees mobile number, email id are registered in the portal of company so that company can communicate with all employees at the same time. Company informs all employees immediately, when events like urgent holiday, client visit, etc. occurs. The employees can receive information in different ways like sms, email, etc.
I will explain this "Observer Design" pattern with the implementation of "Event-Driven Approach". I will not explain it in the mode of object oriented approach. So, it will be little different.
The following are the two basic objects which are involved in the Observer Design pattern:
- Observable (Subject): It provides a way to attach and detach Observer objects. Observer observes it.
- Observer: It is being notified on the change of Subject or Observable.
Observer Design pattern should be used in the following cases:
- When you want to notify the object on the change of another object without knowing about these objects.
Observable/Subject class implementation
- public class MyObservable
- {
- public event EventHandler NotifyToObserver;
- public void EventInObservable()
- {
- EventHandler eventHandler = NotifyToObserver;
- if (eventHandler != null)
- {
- string message = "Information from Observable/Subject";
- eventHandler(message, EventArgs.Empty);
- }
- }
- }
- public class MyObserver
- {
- public void MyObserverEvent(object sender, EventArgs args)
- {
- Console.WriteLine("Subject: {0} ", sender);
- }
- }
- static void Main(string[] args)
- {
- MyObservable myObservable = new MyObservable();
- MyObserver myObserver = new MyObserver();
- myObservable.NotifyToObserver += myObserver.MyObserverEvent;
- myObservable.EventInObservable();
- Console.ReadLine();
- }
Subject: Information from Observable/Subject
Some Condition in the Event-Driven Observer Design Pattern
With an Observer Design Pattern in Object Oriented Approach, any number of Observer objects will listen the changed notification. Observable/Subject doesn't have idea about Observer.
With an Observer Design Pattern in Object Oriented Approach, any number of Observer objects will listen the changed notification. Observable/Subject doesn't have idea about Observer.
But, in an Event-Driven Observer Design Pattern, you basically have one Observer/Consuming object/method and the message what was changed or what happened in the Observable. It doesn't have any number of Observer object.
Lendi VuiPosted Oct 28, 2017, 11:39 AM
Hi, I would like to know if I need to design my API such that when an object is changed its value then the client applications using my API will get notify and the changed value also. Is this Event Driven or Publish Subscribe design pattern? Thanks for you help
Gowtham KPosted Nov 2, 2015, 11:27 AM
Good One
Former memberPosted Nov 2, 2015, 6:48 AM
thanks
Sibeesh VenuPosted Nov 2, 2015, 4:43 AM
Nice Share
Harshad PansuriyaPosted Nov 2, 2015, 1:33 AM
Nice one