/**
*
* 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
/**
* Design Pattern "Observer"
*
* Observateur concret - vue (représente l'état observé)
*
* @author Peter DAEHNE - HEG-Genève
*/
import java.awt.*;
import java.awt.event.*;
public class View1 extends Frame implements Observer {
private Label lblHello;
/* Représente l'état observé (à la construction) */
private void representeInit (State s) {
if (s.langue == State.ENGLISH) {
lblHello = new Label("Hello folks !");
}else {
lblHello = new Label("Salut la compagnie !");;
}
this.add(lblHello, BorderLayout.CENTER);
}// representeInit
/* Représente l'état observé */
private void represente (State s) {
if (s.langue == State.ENGLISH) {
lblHello.setText("Hello folks !");
}else {
lblHello.setText("Salut la compagnie !");
}
}// represente
/* Constructeur */
public View1 (MySubject s) {
addWindowListener(new WindowAdapter() {
public void windowClosing (WindowEvent e) {((Frame)e.getSource()).dispose();}
public void windowClosed (WindowEvent e) {System.exit(0);}
});
setTitle("V1");
s.addObserver(this);
representeInit(s.getState());
pack();
setVisible(true);
}// Constructeur
/* Interface Observer */
public void update (Subject subject) {
represente(((MySubject)subject).getState());
}// update
}// View1
/**
* Design Pattern "Observer"
*
* Observateur concret - vue (représente l'état observé)
*
* @author Peter DAEHNE - HEG-Genève
*/
import java.awt.*;
import java.awt.event.*;
public class View0 extends Frame implements Observer {
private static class MyCanvas extends Canvas {
Image im;
MyCanvas () {}
MyCanvas (Image im) {this.im = im; setSize(im.getWidth(null), im.getHeight(null));}
public void paint (Graphics g) {g.drawImage(im, 0, 0, null);}
}// Canvas
private MyCanvas myCanvas;
private Image imE; /* Image pour ENGLISH */
private Image imF; /* Image pour FRENCH */
/* Représente l'état observé (à la construction) */
private void representeInit (State s) {
if (s.langue == State.ENGLISH) {myCanvas = new MyCanvas(imE);} else {myCanvas = new MyCanvas(imF);}
this.add(myCanvas, BorderLayout.CENTER);
}// representeInit
/* Représente l'état observé */
private void represente (State s) {
if (s.langue == State.ENGLISH) {myCanvas.im = imE;} else {myCanvas.im = imF;}
myCanvas.repaint();
}// represente
/* Constructeur */
public View0 (MySubject s) {
addWindowListener(new WindowAdapter() {
public void windowClosing (WindowEvent e) {((Frame)e.getSource()).dispose();}
public void windowClosed (WindowEvent e) {System.exit(0);}
});
setTitle("V0");
imE = new javax.swing.ImageIcon(System.getProperties().getProperty("user.dir")+"/London.gif").getImage();
imF = new javax.swing.ImageIcon(System.getProperties().getProperty("user.dir")+"/Paris.gif").getImage();
s.addObserver(this);
representeInit(s.getState());
pack();
setVisible(true);
}// Constructeur
/* Interface Observer */
public void update (Subject subject) {
represente(((MySubject)subject).getState());
}// update
}// View0
1 / 3 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !