Welcome to the Design Pattern For Beginners article series. In this article series we are discussing various design patterns of software development. This is the 10th presentation and if you are new to this series then I suggest you go through all previous articles.
- Design Pattern For Beginners - Part 1: Singleton Design Pattern
- Design Pattern For Beginners - Part 2: Factory Design Pattern
- Design Pattern For Beginners - Part 3: Prototype Design Pattern
- Design Pattern For Beginners - Part 4: Decorator Design Pattern
- Design Pattern For Beginners - Part 5: Composite Design Pattern
- Design Pattern For Beginners - Part 6: Adaptor Design Pattern
- Design Pattern For Beginners - Part 7: Bridge Design Pattern
- Design Pattern For Beginners - Part 8: memento Design Pattern
- Design Pattern for Beginners - Part-9: Strategy Design Pattern
Let's discuss the importance of the Observer Design Pattern and when it needs to be implemented.
Why observer pattern?
As the name suggests, it's something related to observation. The question is, who is the observer? The observers are nothing but various systems.
The concept is, one or more systems will be the observer simultaneously and if necessary they can start their action. It's like a bodyguard. Right?
Let's talk about a notification system where the user can send notifications in various ways. They may use SMS notification or Mail Notification or Event Log.
Now, all the notification systems will be alive continuosly, and if needed we can use any one of them, or more than one simultaneously. So , if we draw the conclusion, observer pattern is fit that situation where we choose and use systems at run time. Whereas all systems will alive continuosly. Let's try to implement that in code.
Create various notification classes
We are interested in implementing a uniform naming convention. For that we will implement all notification classes from the INotifyObserver Interface. Each notification class will be implementing a Notify() method.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ObserverPattern
- {
- interface INotifyObserver
- {
- void Notify();
- }
- class MailNotify : INotifyObserver
- {
- public void Notify()
- {
- Console.WriteLine("Notify through Mail");
- }
- }
- class EventNotify : INotifyObserver
- {
- public void Notify()
- {
- Console.WriteLine("Notify through Event");
- }
- }
- class SMSNotify : INotifyObserver
- {
- public void Notify()
- {
- Console.WriteLine("Notify through SMS");
- }
- }
- }


Join the conversation! Your thoughts help the community grow.