Home Segments Top Top Previous Next

767: Mainline

Next, you redefine the setters such that they all include calls to the setChanged and notifyObservers methods, both of which are inherited from the Observable class. Calls to setChanged establish that a change worthy of note has occurred. If a call to setChanged has occurred, establishing that a change worthy of note has occurred, then a call to notifyObservers activates all connected observers, and resets the notification apparatus to its initial no-change-worthy-of-note state. You start to learn how to define observers in Segment 773. You learn how to connect observers in Segment 775.

The Movie class also defines the changed method, which, like the setters, calls setChanged and notifyObservers.

import java.util.*;
public class Movie extends Observable implements MovieInterface {
 // Define instance variables:
 private String title, poster;
 private int script = 5, acting = 5, direction = 5;
 // Constructors as defined in Segment 766
 // Define setters:
 public void setScript(int s) { 
  script = s;                   
  setChanged();                 
  notifyObservers();            
 }                              
 // Analogous setters defined for acting and direction
 // Getters as defined in Segment 766
 // rating method as defined in Segment 766
 public void changed () {       
  setChanged();                 
  notifyObservers();            
 }                              
}