The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. This model of sending and receiving notifications is also called the publisher-subscriber-model. One or more subscribers objects subscribe to receive notifications from the publisher and the publisher sends out notifications to all the subscribed objects.
Let's see the diagram below
You can see the IObserver interface has the method, update(), that gets called when the Subject's state changes. Like refreshing the user interface, saving something in the database, and so on.
The Observer class can be any that implements the interface IObserver. Each observer registers with a subject to receive updates.
Objects can use the interface ISubject to register as observers and to remove themselves from being observers. The attach() and detach() method subscribe and unsubscribe the observers and the notify() method sends the notification to the subscribers.
The subject always implements the interface ISubject. In addition to the attach() and detach() method, the subject implements the notify() method that is used to update all the current observers whenever the state changes.
Let's illustrate an example.
Let's say you are building a forum but to ensure quality and to avoid marketing spam, an administrator will monitor the forum posts. Once a post arrives in the system you need to notify the administrator about the new post.
The IForumObserver interface is the interface that would be implemented by the observer classes, AdminObserver and ActivityObserver.
The AdminObserver class is interested in getting notified when a new forum post is made.
The IForumNotifier interface is implemented by the publishers of notifications in this case the ForumNotifier.
The ForumNotifier class would implement the interface IForumNotifier.
Let's see some code
The IForumObserver interface contains the update() method, which receives a ForumPost object as its parameter.
public interface IForumObserver {
public void update(ForumPost post);
}
The AdminObserver class implements the update() method defined in the interface IForumObserver interface.
public class AdminObserver implements IForumObserver{
@Override
public void update(ForumPost post) {
System.out.println("Forum post " + post + " received");
}
}
The IForumNotifier interface defines subscribe(), unsubscribe() and notify() methods. Where the subscribe() and unsubscribe() methods accept an IForumObserver object as their parameters and the notify() method a ForumPost.
public interface IForumNotifier {
public void subscribe(IForumObserver observer);
public void unsubscribe(IForumObserver observer);
public void _notify(ForumPost post);
}
You can see the ForumNotifier class implements the IForumNotifier interface. The subscribe method adds the IForumObserver object received as the parameter. The unsubscribe() method removes the specified IForumObserver object received from the parameter. And the notify() method updates the observer.
public class ForumNotifier implements IForumNotifier{
@Override
public void subscribe(IForumObserver observer) {
System.out.println(observer + " was added");
}
@Override
public void unsubscribe(IForumObserver observer) {
System.out.println(observer + " was removed");
}
@Override
public void _notify(ForumPost post) {
System.out.println(post + " was updated");
}
}
Reference
Joshi, B. (2016b). Beginning SOLID Principles and Design Patterns for ASP.NET Developers (English Edition) (1st ed.). Apress.
Comments
Post a Comment