I used the template pattern for an algorithm I’m writing. But I combined it with the observer pattern to get informed about the process.
public abstract class Test extends Observable
In the method for the algorithm i call the notify
public final void findSolution() {
// run algorithm
notifyObservers();
}
This abstract class with the findSolution method will be extended by different algorithm implementations. The UI just keeps a reference to the Test and calls the findSolution() but the update method from the ui will never be invoked does anybody now what is wrong?
public class CreateViewResults implements Observer {
private Test algorithm;
public CreateViewResults() {
algorithm = new OneTestImpl();
algorithm.addObserver(this);
algorithm.findSolution();
}
@Override
public update( Observable ob, Object o ) {
System.out.println("Update");
}
}
Nothing happens because the observable hasn’t changed.
From the javadoc:
You should thus call
setChanged()before notifying the observers.