KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > Observable

java.util
Class Observable

java.lang.Object
  extended by java.util.Observable
See Also:
Top Examples, Source Code, notifyObservers(), notifyObservers(java.lang.Object), Observer, Observer.update(java.util.Observable java.lang.Object)

public void addObserver(Observer o)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1835]Using Observable Class and Observer Interface
By anuragmishra1005 { at } yahoo { dot } com on 2006/10/26 06:24:02  Rate
There are two java programs here one is observerSubject.java and another  
 is observerClient1.java. 
  
  
 observerSubject 
  This class extends the Observable class and gets all the property of Observable class. 
  This class then creates an instance if "observerClient1" and adds this as an Observer. 
  In the program the loop is from 0 to 5 and for even value of i, the message of Object 
  "observerSubject" gets changed and its notified to the "observerClient1". 
  To notify the change to the "observerClient1" this class will call the setChanged (  )  method 
  which denotes the Object which is being observed is changed and also it uses the method 
   notifyObservers ( Object arg )   to pass the changed object to the "observerClient1". 
  
  
 observerClient1 
  This class implements the Observer interface and it must provide implementation for method 
  public void update ( Observable o, Object arg )  {  }  and the control comes here when the notification is  
  given by the observerSubject and we can call another methods from here or implement the functionality 
  which we want to do the change of the Object which this class was Observing. 
  
  
  
 /********** observerClient1.java ---STARTS -------- ***********/ 
 import java.util.Observable; 
 import java.util.Observer; 
  
  
 public class observerClient1 implements Observer {  
  
  
   public void update ( Observable o, Object arg )   {  
     observerSubject obsSubjectChanged =  ( observerSubject ) arg; 
     checkForChange ( obsSubjectChanged.getMessage (  )  ) ; 
     //System.out.println ( "The update method is called in observerClient1:::"+obsSubjectChanged.getMessage (  )  ) ; 
    }  
  
  
   public void checkForChange ( String changedMessage )  {  
     System.out.println ( "The changed message is::"+changedMessage ) ;     
    }  
  }   
 /********** observerClient1.java ---ENDS -------- ***********/ 
  
  
  
 /********** observerSubject.java ---STARTS -------- ***********/ 
 import java.util.Observable; 
  
  
 public class observerSubject extends Observable {  
    
   private String message="Hello";//This is the observerSubject property which will be changed. 
    
   public static void main ( String [  ]  args )   {  
     observerSubject obsSubject = new observerSubject (  ) ; 
     observerClient1 obzClient1 = new observerClient1 (  ) ; 
     obsSubject.addObserver ( obzClient1 ) ; 
     for  ( int i = 0; i  <  5; i++ )   {  
       if ( i%2 == 0 )  { //print only when i is even i.e 0,2,4.. 
         obsSubject.message = "hello222--"+i; 
         obsSubject.setChanged (  ) ; 
         obsSubject.notifyObservers ( obsSubject ) ; 
        }        
      }  
    }  
   //these are getter methods to get value in client class. 
   public String getMessage (  )   {  
     return message; 
    }  
   public void setMessage ( String message )   {  
     this.message = message; 
    }  
  }  
  
  
  
 /********** observerSubject.java ---ENDS-------- ***********/ 
  
  
 


protected void clearChanged()
See Also:
notifyObservers(java.lang.Object), notifyObservers()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int countObservers()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void deleteObserver(Observer o)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void deleteObservers()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean hasChanged()
See Also:
setChanged(), clearChanged()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void notifyObservers()
See Also:
Observer.update(java.util.Observable, java.lang.Object), hasChanged(), clearChanged()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void notifyObservers(Object arg)
See Also:
Observer.update(java.util.Observable, java.lang.Object), hasChanged(), clearChanged()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Observable()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected void setChanged()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags