This post is next in series of Design Patterns. This one describes mainly about Observer design Pattern. But before going deep into it, lets see how Design patterns are basically classified.
A book called Design Patterns – Elements of reusable Object-oriented software came up in 1994, written by 4 authors Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides who are commonly known as Gang of Four. Till now this book is described as the best work in Design Patterns. Gang of Four suggested 24 Design Patterns in 3 categories:
This post puts more focus on OBSERVER PATTERN which falls under Behavioral Design Patterns since it focuses on relationship between Objects (1:M)
This pattern is all about 1:M dependency between objects so when one object changes state, all its dependencies are notified & updated automatically. All of us must be familiar with Publish & subscribe relationship. Here Publisher is referred to as subject & Subscribers as observers. So whenever Publisher publish anything, all the subscribers gets the notification intimating that publisher has published or pushed a new thing. Here Subscribers do not need to ping publisher at regular interval for getting any new update, instead publisher notify all the subscribers in case there is any new update.
There are many real life examples where we can see this pattern being followed like : Twitter : here once you follow a particular user, all followers of that user will be notified for any new tweet or activity of that User. Another example is that of Push Notifications. All of us have been using android phones. Lets take an example of News App, this app will notify all its Users whenever a new news arrives. or incident occurs.
If you notice in WordPress even there is a follow button for every Blog. If you follow a blog, then you will be notified for any activity of that blog like new post etc. The code snippet which I will be sharing here to better understand Observer pattern is about the same Blog-User Scenario. Here Every subscriber of the blog will be notified whenever a new article is added to Blog. Lets step into it.
I have created a console application and added below 4 Entities:
IObserver: This is an Interface having common method prototype so that all observers can inherit from it to give their own implementation for this common method.
Blog: Concrete Subject, This is the actual subject which will be inherited from Subject abstract class to have access to all predefined events. Notifies its observers (Blog followers) when a new article added to blog.
BlogFollower: This is the concrete observer i.e. the actual observer who will implement IObserver interface and give method implementation. Maintains the state consistent to Blog.
I will be using Events & Delegates here for Subscribing & unsubscribing. Events are themselves a very good example of observer pattern as any event handler which is attached to a particular event will be notified whenever that event occurs.
here you will notice that this class has all needed properties required for a Blog like: Name, author of Blog & name of New article for same blog.
Have events like Subscribe: through which a new user can subscribe to blog, Unsubscribe: though which an existing subscribed user can be unsubscribed from blog and a Notify method: though which subscribed users will be notified for a new article added to blog.
This basically acts as a template for every observer that here refers as Blog Subscriber. It has the name property and an update method to update the observer every time subject notifies for new article.
Here the values for properties of abstract class are being assigned using constructor, and a new method NewArticle is created which assigns name to artcle (NewArtcleName property of Subject) and calls its Notify Method.
This also have a constructor to assign values to Interface properties and a method to update the Observer.
Here I have an object of Blog class (i.e. Concrete Subject). Then I have two objects of BlogFollower class (subscribers/observers). Then I subscribe both the users to the blog. Then a new article is being added to Blog. So as soon as new article is being added, both the followers i.e. User A & User B should be notified for new article. Now I unsubscribe User A. Again add a new article but now only User B should be notified for new article since User A is already unsubscribed. Now I unsubscribe User B as well. Lets see what output screen looks like
Here you can see first both users are subscribed to blog, then notified for new article and then one is unsubscribed, other still be notified for any new article.
This was my try on Observer Pattern. In case you want to have a look on source code, please go ahead and download from here. Will discover other patterns in my next post in series of Design Patterns.