Class Diagram : Observer

Class Diagram - Observer

Let us now look into the generic class diagram for the observer . In this case instead of using the term subscriber , we will use the much generic term Observer. So we will have an interface for the Observer. Also instead of having a method called haveEMailID(), we will have a much generic method called as update(). Update method is a mechanism provided by the observer so that the system can update them about the changes.

Figure- Figure

Also instead of using the word Topic I will use the much generic term Subject with methods addObserver(Observer), removeObserver(Observer) and notify().

The implementation of addObserver is very simple blindly add the observer passed as an argument in to the collection.

addObserver(Observer ob)

The implementation of removeObserver is very simple blindly remove the observer passed as an argument in to the collection.

removeObserver(Observer ob)

The implementation of notify is very simple for all observers in the collection , call the update method on the same.

Apart from the Observer interface, there will be multiple ConcreteObservers who will be implementing the Observer interface and hence will override the update method with their own implementations for the same.

Similarly there will be multiple ConcreteSubjects again implementing the Subject interface overriding all the methods and providing implementations for the same. Each ConcreteSubjects will basically have different states and hence they should have the getState() and setState(State) method which allows to get and set the state of a Subject.

The implementation of getState is very clear, whatever be the current state of the subject return it to the caller while the implementation of setState is .. whatever state is passed to this method populate it on the subject and call the notify method which in turn calls the update method on all the observers associated to this collection.

So whenever a ConcreteObserver wants to associate with a ConcreteSubject it invokes the addObserver method of the ConcreteObserver which in turn adds it to the collection. If a ConcreteObserver wants to publish some latest information, it calls the setState method of that ConcreteSubject passing the new state as an argument wherein first the latest state is set on the ConcreteSubject and then the notify method of the ConcreteSubject is called. The notify method inturn calls the update method of all the Observers in the collection notifying them of the change in state.

The update method of the observers can come in two form a parameterized update method and the non parameterized update method.

Parameterized update method should only be used if the latest state on the subject needs to be passed to all the observer irrespective of the fact whether they are interested or not. The problem with the parameterized update method is if there are large number of observers to be notified and if the size of the state is huge , then a huge amount of network bandwidth might get hogged.

The non parameterized update method is more efficient , wherein the ConcerteSubject only notifies the ConcreteObserver that the state on the ConcerteSubject has changed and if the ConcreteObserver is interested in the latest state, it can get the same by calling the getState() of the ConcreteSubject.

Hemant Jha
Founder - VPlanSolutions
Researcher, Trainer