In this article I will discuss about the observer pattern in C# with simple code example. I will use the publisher subscriber mechanism of the delegates to implement the observer pattern.
What is Observer Pattern ?
Before starting the discussion about the observer pattern let me brief about the pattern. In this pattern when the state of one object(observer) is changed that changed is propagated to all the dependent objects.
In this article I will use delegates to implement this pattern. I have discussed about delegates here.
UML Diagram
Please have a look at the below figure for the UML diagram.
Publisher: This is the class which keeps the list of all the subscribers with the help of the delegate instance.
public delegate void Notifier(string message); public class Publisher { Notifier notifier; public void Attach(Notifier notifier) { this.notifier += notifier; } public void Remove(Notifier notifier) { this.notifier -= notifier; } public void SendMessgae(string message) { if (this.notifier != null) notifier(message); } }
We can safely use the delegate here as we know that delegate class contains invocation list and we can attach and detach the methods to the invocation list as shown in the Attach and Remove methods.
Observer :
Observer is the class which has the instance of the Publisher class and whenever any change is happened in the instance of this class. The publisher is notified which in turn notifies all the subscribers. The code for Observer is as following.
public class ArticleServiceObserver { private Publisher publish; public ArticleServiceObserver(Publisher publish) { this.publish = publish; } public void AddNewArticle(string articleName) { publish.SendMessgae("New Article Available: " + articleName); } }
Subscriber :
This is the class which is subscribed to the publisher to receive notifications. In our case Person class is the subscriber, where the instances of this class will receive the notifications for the newly published articles.
public class Person { private string name; public Person(string name) { this.name = name; } public void GotArticle(string message) { Console.WriteLine(string.Format("Hello {0}, {1}", name, message)); } }
Now its time to see our observer pattern in use.
static void Main(string[] args) { Publisher pub = new Publisher(); Person per1 = new Person("Michale"); pub.Attach(per1.GotArticle); Person per2 = new Person("Jordon"); pub.Attach(per2.GotArticle); ArticleServiceObserver observer = new ArticleServiceObserver(pub); observer.AddNewArticle("Delegates in C#"); pub.Remove(per2.GotArticle); observer.AddNewArticle("Observer pattern"); Console.Read(); }
In the above code we can see that I have created two persons and subscribed the GotArticle() method of each instance to the publisher. Now when “Delegates in C#” is published both the person will receive the notification. Now suppose if one of the person wants to unsubscribe, in this “Jordon” has done it. When the second article is published only “Michale” will receive the notification as shown in the figure below.
Conclusion
In this article we have seen how easily we can implement observer pattern using delegates in C#.
Dmitry Bytchenko says
Typo “public void SendMessgae(string message)” instead of “SendMessage”