I want to know if the observer pattern configered with a template observer is more convenient than the general observer pattern. The concept of a template observer is a observable class which has a specific observer type configered as a template. As an example I will show a normal observer and compare it with a template observer.
Normal observer pattern
Normal observer interface:
public interface NormalObserver {
//Update method
void update(final String updateIn);
}
The update function is the same for every class that implements the NormalObserver interface. This causes problems when you want to implement a specific observer, for example an observer with different update parameters.
Concrete observer:
public class ConcreteNormalObserver implements NormalObserver {
//Update implication for NormaObserver interface
@Override
public void update(String updateIn) {
System.out.println(updateIn);
}
}
Normal observable interface:
public interface NormalObservable {
//method for adding an observer
void addObserver(NormalObserver observerIn);
//method for removing an observer
void removeObserver(NormalObserver observerIn);
//method for notifying observers
void notifyObservers();
}
Concrete normal observable:
public class ConcreteNormalObservable implements NormalObservable {
//List for Observer
private List<NormalObserver> mObservers;
public ConcreteNormalObservable() {
this.mObservers = new ArrayList<>();
}
@Override
public void addObserver(NormalObserver observerIn) {
if(observerIn != null && !this.mObservers.contains(observerIn)) {
this.mObservers.add(observerIn);
}
}
@Override
public void removeObserver(NormalObserver observerIn) {
this.mObservers.remove(observerIn);
}
@Override
public void notifyObservers() {
for(NormalObserver lObserver: this.mObservers) {
lObserver.update("Update");
}
}
}
This is in my opinion a straightforward observer pattern, you could argue that the notifyObservers method is a bit redundant because an observable could just call the update method from its observers anywhere.
Template observer pattern
Template observable interface:
/**
* @param <ObserverType> Concrete observer to implement in observable,
* this ObserverType specifies the update functions which the TemplateObservable can call.
*/
public interface TemplateObservable<ObserverType> {
/**
* Function to add an Observer of a specific type
* @param observerIn
*/
void addObserver(ObserverType observerIn);
/**
* Function to remove an Observer of a specific type
* @param observerIn
*/
void removeObserver(ObserverType observerIn);
}
The template type defines which observer type you are going to use, so a concrete observable specifies which observer it is going to use.
Concrete observable template:
/**
* Concrete example of a class that implements TemplateObservable.
* This class is uses a ConcreteTemplateObserver as its observerType.n
*/
public class ConcreteTemplateObservable implements TemplateObservable<ConcreteTemplateObserver> {
//List to hold all the observers
private List<ConcreteTemplateObserver> mObservers;
public ConcreteTemplateObservable() {
this.mObservers = new ArrayList<>();
}
@Override
public void addObserver(ConcreteTemplateObserver observerIn) {
//Check for non null and if the observer parameter is not already in the observerList
if(observerIn != null && !this.mObservers.contains(observerIn)) {
this.mObservers.add(observerIn);
}
}
@Override
public void removeObserver(ConcreteTemplateObserver observerIn) {
this.mObservers.remove(observerIn);
}
//Own update function, no need for notify function, but can be optional.
public void update() {
for(ConcreteTemplateObserver lObserver: this.mObservers) {
lObserver.updateTemplateObserver("Update");
}
}
}
Concrete template observer:
public class ConcreteTemplateObserver {
public void updateTemplateObserver(final String messageIn) {
System.out.println(messageIn);
}
}
I am convinced that the template observer pattern is more usefull because of the specific update functions you can call from the observable whereas in the normal observable you are forced to use the normal update function, but I am doubtfull if the template observable meets the criteria of an observer pattern.