Please enable Javascript to correctly display the contents on Dot Net Tricks!

Observer Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 23 Feb 2017
Total Views : 2,561   
Updated On : 23 Feb 2017
 

Observer Design Pattern falls under Behavioral Pattern of Gang of Four (GOF) Design Patterns in .Net. The command pattern is commonly used in the menu systems of many applications such as Editor, IDE etc. In this article, I would like share what is observer pattern and how is it work?

What is Observer Design Pattern?

This pattern is used when there is one to many relationship between objects such as if one object is modified, its dependent objects are to be notified automatically.

This pattern is allows a single object, known as the subject, to publish changes to its state and other observer objects that depend upon the subject are automatically notified of any changes to the subject's state.

Observer Design Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the Observer Design Pattern is given below:

Observer Design Pattern C#

The classes, interfaces and objects in the above UML class diagram are as follows:

  1. Subject

    This is a class that contains a private collection of the observers that are subscribed to a subject for notification by using Notify operation.

  2. ConcreteSubject

    This is a class that maintains its own state. When a change is made to its state, the object calls the base class's Notify operation to indicate this to all of its observers.

  3. Observer

    This is an interface which defines an operation Update, which is to be called when the subject's state changes.

  4. ConcreteObserver

    This is a class that implements Observer interface and examines the subject to determine which information has changed.

C# - Implementation Code

public abstract class Subject
{
 private ArrayList observers = new ArrayList();

 public void Attach(IObserver o)
 {
 observers.Add(o);
 }

 public void Detach(IObserver o)
 {
 observers.Remove(o);
 }

 public void Notify()
 {
 foreach (IObserver o in observers)
 {
 o.Update();
 }
 }
}

public class ConcreteSubject : Subject
{
 private string state;

 public string GetState()
 {
 return state;
 }

 public void SetState(string newState)
 {
 state = newState;
 Notify();
 }
}

public interface IObserver
{
 void Update();
}

public class ConcreteObserver : IObserver
{
 private ConcreteSubject subject;

 public ConcreteObserver(ConcreteSubject sub)
 {
 subject = sub;
 }

 public void Update()
 {
 string subjectState = subject.GetState();
 Console.WriteLine(subjectState);
 }
} 

Real Life Example:

Real Life Example of Observer Design Pattern C#

When to use it?

  1. Changes in state of an object need to be notified to a set of dependent objects, not all of them.

  2. Notification capability is required.

  3. The object sending the notification does not need to know about the receivers objects.

What do you think?

I hope you will enjoy the Observer Design Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



ABOUT AUTHOR

Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

Free Interview Books
 
COMMENTS (0)
2 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST

Know More
28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

Know More
27 MAY
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

Know More
25 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
25 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:00 PM-6:30 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

22 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM Weekend

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM Weekend

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+