• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Observer Pattern implementation problem. Help needed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
***/
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

It looks like one observer is getting a singleton instance of the model via a getInstance method, while the other is creating a new one. Can you check to make sure there is only one created, and both observers add themselves to the observer list of the same model instance?

BTW: The management will be around shortly to ask you to re-read the rules at registration and fix your name up to be a believable "first last" kinda name.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic