34. OBSERVER( Ejemplo ) public abstract class Subject { private List<Observer> observers; public void add(Observer o) { this .observers.add(o); } public void remove(Observer o) { this .observers.remove(o); } public void notify() { for ( int i = 0; i< this .observers.size(); i++) this .observers.get(i).update(); } public Subject() { this .observers = new ArrayList<Observer>(); } } public abstract class Observer { private Subject subject; protected Subject getSubejct() { return this.subject; } public Observer(Subject s) { this.subject = s; } public abstract void update(); }
35. OBSERVER( Ejemplo ) public class Dominio extends Subject { private double a , b , c ; public Dominio( double [3] s) { super (); this .a = s[0]; this .b = s[1]; this .c= s[2]; } public double [3] getState() { double [3] s = new double [3]; s[0] = this .a; s[1] = this .b; s[2] = this .c; return s; } public void setState( double [3] s) { this .a = s[0]; this .b = s[1]; this .c= s[2]; this .notify(); } } public class Vista1 extends Observer { private double [][] tabla = new double [3][3]; private int sig = 0; public Vista1(Dominio d) { super (d); this .update(); } public void update() { double [3] s = ((Dominio).getSubject()).getState(); this .tabla[sig] = s; sig = (sig+1)%3; this .redibujar(); } }
36. OBSERVER( Ejemplo ) public class Vista2 extends Observer { private double a , b , c ; public Vista2(Dominio d) { super (d); this .update(); } public void update() { double [3] s = ((Dominio)getSubject()).getState(); a = s[0]; b = s[1]; c = s[2]; this .redibujar(); } } public class Vista3 extends Observer { private double a , b , c ; public Vista3(Dominio d) { super (d); this .update(); } public void update() { double [3] s = ((Dominio)getSubject()).getState(); a = s[0]; b = s[1]; c = s[2]; this .redibujar(); } }