This pattern falls under the category of behavioral pattern. As the name suggests, it is based on the principle where one object observes another object.
So, in simple words, this pattern defines a one-to-many relationship between subject and observer objects so that if the subject changes its state, then all of the dependent observer objects will be notified of the change.
Let’s understand it in more details using the UML diagram, as shown below.
Image source: Wikipedia
For example
Consider a technical author like we have in C# Corner, who writes blogs, articles etc. So, if someone likes reading articles from a specific author, one can follow/subscribe the author for future articles. This is a real-life scenario of the Observer pattern.
In this case, all the subscribers who want to subscribe to the author are Observer Objects and the author is a Subject.
Now, let’s dive into the actual code to see how it works.
This is the interface which will be implemented by the subject.
- public interface ISubject {
- void registerObserver(Observer observer);
- void unregisterObserver(Observer observer);
- void notifyObservers();
- }
This is the Subject class and as per our example mentioned above, the author will be the Subject. So here, we are assuming that the author has already written 1 article and those who will like the article can subscribe or unsubscribe (if they have already subscribed) accordingly.
Now, whenever the subject changes its state, all of its observers will be notified.
- public class Subject: ISubject {
- private List < Observer > Observers = new List < Observer > ();
- private int articlesCount = 1;
- public int Articles {
- get {
- return articlesCount;
- }
- set {
- if (value > articlesCount) {
- articlesCount++;
- notifyObservers();
- }
- }
- }
- public void registerObserver(Observer observer) {
- Observers.Add(observer);
- }
- public void unregisterObserver(Observer observer) {
- Observers.Remove(observer);
- }
- public void notifyObservers() {
- foreach(var observer in Observers) {
- observer.Update();
- }
- }
- }
This is the interface which will be implemented by all the Observer objects. As per our example, all the subscribers/followers for the author will be the Observer objects.
- public interface IObserver {
- void Update();
- }
This is the Observer class which can be used to create different observers.
- public class Observer: IObserver {
- public string ObserverName;
- public Observer(string name) {
- ObserverName = name;
- }
- public void Update() {
-
- Console.WriteLine("Hello " + ObserverName + ", a new article has been published by the author.");
- }
- }
Finally, this is the main method which can be used for running this program.
- void Main() {
- var subject = new Subject();
- var observerA = new Observer("Observer A");
- var observerB = new Observer("Observer B");
- var observerC = new Observer("Observer C");
- Console.WriteLine("Intially suppose Subject has already written total " + subject.Articles + " article");
- Console.WriteLine("\n*******************************************************************************\n");
- Console.WriteLine("Registering observers A and B for future articles...............");
- subject.registerObserver(observerA);
- subject.registerObserver(observerB);
- Console.WriteLine("New article published by Subject,so now observers A and B will be notified....\n");
- subject.Articles++;
- Console.WriteLine("---------------------------------------------------------------------------\n");
- Console.WriteLine("Registering observer C for future articles and unregistering observer B from the future articles...............");
- subject.registerObserver(observerC);
- subject.unregisterObserver(observerB);
- Console.WriteLine("New article published by Subject,so now observers A and C will be notified....\n");
- subject.Articles++;
- Console.WriteLine("\n*******************************************************************************\n");
- Console.WriteLine("Finally Subject has written total " + subject.Articles + " article \n");
- }
OUTPUT
When should we use this
We should use it when multiple objects are dependent on the state of one object.
Some common Use Cases
- Following someone on Instagram/Twitter and other such platforms.
- App users gets notified for the updates.
- Email Subscriptions and many more.
I hope you find this article helpful. Stay tuned for more … Cheers!!