Hi there!
We have two view classes and one Model class. Views will get the data from the model. The problem we're facing is that we're not able to register the one of the view classes to the model.
The model classes is DefectLogModel
The view classes are DefectLogView and EffortLogView.
When we try to register both views, only DefectLogView is being registered and hence its getting all the update methods. Where as the EffortLogView is not being updated. When we try to see the number of number of Observers, its giving the result as "1" instead of 2.
Can any one suggest how to find out what classes are registered? Is there any other approach to register more than one view to the model?
The below is the segment of code that implements the Observer
pattern.
DefectLogModel.java
-------------------
public class DefectLogModel {
private ArrayList listDefectLogDO;
private boolean hasChanged;
public ChangeNotifier cNotify;
.......
public class ChangeNotifier extends Observable{
public void notifyObservers(){
try{
cNotify.setChanged();
if(hasChanged && cNotify.hasChanged()){
super.notifyObservers(getAllDefects());
hasChanged = false;
cNotify.clearChanged();
}
}
catch(Exception e){
System.out.println("erro calling updates" );
e.printStackTrace();
}
}
}
public Observable change(){
return cNotify;
}
//Class constructor
public DefectLogModel(){
taskid = new
String("task1");
dbBroker = DbBroker.getDBInstance();
listDefectLogDO = dbBroker.getAllDefects();
currentDefectLogDO = new DefectLogDO();
newDefectLogDO = new DefectLogDO();
hasChanged = false;
cNotify = new ChangeNotifier();
}
.......
/**
No problem with below class. DefectLogView gets notified if there are any changes in the DefectModel.
**/
DefectLogView.java
------------------
public class DefectLogView{
private ChangeObserver changeObserver;
private class ChangeObserver implements Observer {
public void update(Observable ob, Object o) {
loadList();
}
}
//Class Constructor
public DefectLogView() {
currentDefectLog = null;
defectLogModel = DefectLogModel.getDLInstance();
changeObserver = new ChangeObserver();
defectLogModel.change().addObserver(changeObserver);
}
....
/**
We're facing problem in registering this class to the DefectLogModel. No updateas are being notified to this class.
**/
EffortLogView.java
------------------
public class EffortLogView{
private DefectLogModel dLModel;
private ChangeObserver cObserver;
private boolean hasChanged;
private class ChangeObserver implements Observer {
public void update(Observable arg0, Object arg1) {
defectList = (ArrayList) arg1;
loadDefectList(defectList);
}
}
public Observable cd() {
return cdNotify;
}
/** Constructor */
public EffortLogView() {
effortLogModel = new EffortLogModel();
dLModel = new DefectLogModel();
cObserver = new ChangeObserver();
dLModel.change().addObserver(cObserver);
}
.....
/***
In the above code, we're registering the EffortLogView using the line
"dLModel.change().addObserver(cObserver). But we dont find the notification to this class.
***/