• 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

Observable not working properly...

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following piece of code...
import java.util.*;
public class TestObs{
public static void main(String[] a){
Worker worker = new Worker();
supervisor shekar = new supervisor();
worker.addObserver(shekar);
worker.i=50;
System.out.println(" Has change :"+worker.hasChanged());
}
}

class supervisor implements Observer {
public void update(Observable o , Object arg){
System.out.println("Object has changed");
}

}
class Worker extends Observable{
public int i=10;
public void notifyObservers(){
System.out.println("change has occured in the observed object");
}
}

in the above code i have changed the data in the observable object worker, i.e the value of i to 50. This is a change in the data of the observable object. When i do that the notifyObservers should run and the hasChanged method should return a true value but the value that is returned is false.
How can I correct my method or is there something missing with my understanding. Kindly help me out if I have understood the observable correctly.
Thanks and regards.
shekar.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The javadocs are not a model of clarity on this, but you need to setChanged() in your Observable and then you need to actually notifyObservers(). It's in the notifyObservers that you need to call hasChanged() and clearChanged() but the simplest way to achieve this is to call super.notifyObservers() when you over-ride. So for example have a setValue method in Worker to handle your change to variable i:

This will call your update method in Supervisor.
Regards,
Kenny
 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply
It is working fine. but i still have some problems with the following code.
import java.util.*;
public class TestObs{
public static void main(String[] a){
Worker worker = new Worker();
supervisor shekar = new supervisor();
worker.addObserver(shekar);
supervisor shekar1 = new supervisor();
worker.addObserver(shekar1);
supervisor shekar2 = new supervisor();
worker.addObserver(shekar2);

System.out.println(" No of observers :"+worker.countObservers());

worker.setValue(50);
System.out.println(" Has change :"+worker.hasChanged());
}
}

class supervisor implements Observer {
public void update(Observable o , Object arg){
System.out.println("Object has changed");
}

}
class supervisor1 implements Observer {
public void update(Observable o , Object arg){
System.out.println("Object has changed1");
}

}
class supervisor2 implements Observer {
public void update(Observable o , Object arg){
System.out.println("Object has changed2");
}

}
class Worker extends Observable{
public int i=10;
public void setValue(int val)
{
i=val;
setChanged();
// System.out.println(" Has change :"+worker.hasChanged());
notifyObservers();
}
public void notifyObservers()
{
super.notifyObservers();
System.out.println(" Look boss, I'm doing some work!");
}
}

In the above code when I have added more than one observer for all the observers the update method in supervisor is getting called. I guess this is wrong because the corresponding observer's method should be called. Also when I say hasChanged it is giving the value false.
What could be problem and will update of every observer execute or only the update of particular observer exectutes for the changes .

thanks and regards,
shekar.
 
KR Campbell
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've probably already worked it out;
anyway, according to your code you have created three instances of supervisor rather than one of each of your three different types of observers.
Regards,
Kenny
 
KR Campbell
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, didn't answer your question about hasChanged.
Remember that super.notifyObservers() checks hasChanged() and then clears it. This means that in setValue after setChanged() a call to this.hasChanged() will return true. (your code has worker.hasChanged() which won't compile if you un-comment it)
However, in your main method a call to worker.hasChanged() will return false after setValue because the call to notifyObservers() in setValue has already cleared the change.
Regards,
Kenny
reply
    Bookmark Topic Watch Topic
  • New Topic