开发者

Observer combined with Template Pattern

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 alg开发者_如何转开发orithm 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:

If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed.

You should thus call setChanged() before notifying the observers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜