Home Segments Top Top Previous Next

776: Mainline

The new, more complex, setter does nothing if the new movie is the same as the previous movie. If the previous movie assignment is an instance of the Movie class (that is, if it is not null), the setter disconnects that previous movie from all previously connected observers using deleteObservers, which is a method inherited from the Observable class. Then, the setter assigns the new movie to the movie variable, adds a new observer if the new movie is an instance of the Movie class, and calls changed to activate the newly connected, new observer.

public void setMovie (Movie m) { 
 if(movie == m) {return;}                                       
 if(movie instanceof Movie) {movie.deleteObservers();} 
 if(m instanceof Movie) { 
  movie = m; 
  movie.addObserver(new LocalMovieObserver()); 
  movie.changed(); 
 } 
}