/**
*
* Programme principal illustrant le Design Pattern "Observer"
*
* @author Peter DAEHNE - HEG-Genève
*/
public class Principal {
public static void main (String[] args) {
State state = new State();
state.langue = State.FRENCH;
MySubject subject = new MySubject(state);
new Change(subject).setVisible(true);
Observer o1 = new View0(subject);
Observer o2 = new View1(subject);
}// main
}// Principal
/**
* Design Pattern "Observer"
*
* État d'un sujet concret
*
* @author Peter DAEHNE - HEG-Genève
*/
public class State {
public static final int ENGLISH = 1;
public static final int FRENCH = 2;
public int langue = FRENCH;
}// State
/**
* Design Pattern "Observer"
*
* Classe modifiant l'état observé
*
* @author Peter DAEHNE - HEG-Genève
*/
import java.awt.*;
import java.awt.event.*;
public class Change extends Frame {
private MySubject mySubject;
/* Constructeur */
public Change (MySubject s) {
mySubject = s;
addWindowListener(new WindowAdapter() {
public void windowClosing (WindowEvent e) {((Frame)e.getSource()).dispose();}
public void windowClosed (WindowEvent e) {System.exit(0);}
});
setTitle("Change");
Button btn = new Button("Modifie état");
btn.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
State s = mySubject.getState();
if (s.langue == State.ENGLISH) {
s.langue = State.FRENCH;
}else {
s.langue = State.ENGLISH;
}
mySubject.setState(s);
}// actionPerformed
});
add(btn, BorderLayout.CENTER);
pack();
setVisible(true);
}// Constructeur
}// Change