Aim is to tear apart domain (model) and gui (view) layer.
Question how can the view know about changes of the model?
Gui needs to implement the Observer-interface. By doing so, it will need to implement a update() method.
The domain, on the other side, needs to inherit of the superclass ‘Observable’. By doing so, it inherits all important methods to register observers, track changes and inform observers about these.
- addObserver (Observer o);
- notifyObservers(Object arg);
Last but not least, the model needs to register the observer.
Thus, 3 steps are necesarry: 1. View implements Observer interface 2. Model inherits from Observable class 3. Model registers the view as oberver
The observer receives two important things from the domain object: a reference to the domain object and optionally the argument. In this way, maximally three possibilities exists how the gui can make get info to change view accordingly
- By a type-casted get-method call to observer reference ((CustomClass)o).getInfo();
- By type-casting the argument: (Integer)arg
- (In case observer is also added as instance to gui) by call to domain get-method