xxxxxxxxxx
class Signal extends Observable {
void setData(byte[] lbData){
setChanged(); // Positionne son indicateur de changement
notifyObservers(); // (1) notification
}
}
xxxxxxxxxx
// Java Observer pattern (Simple version)
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
public interface Observer {
public void update(int value);
}
public class SimpleSubject implements Subject {
private List<Observer> observers;
private int value = 0;
public SimpleSubject() {observers = new ArrayList<Observer>();}
public void registerObserver(Observer o) {observers.add(o);}
public void removeObserver(Observer o) {observers.remove(o);}
public void notifyObservers() {
for (Observer observer : observers) {
observer.update(value);
}
}
public void setValue(int value) {
this.value = value;
notifyObservers();
}
}
public class SimpleObserver implements Observer {
private int value;
private Subject simpleSubject; // who to subscribe
public SimpleObserver(Subject simpleSubject) {
this.simpleSubject = simpleSubject;
simpleSubject.registerObserver(this);
}
public void update(int value) {
this.value = value;
display();
}
public void display() {System.out.println("Value: " + value);}
}
public class Example {
public static void main(String[] args) {
SimpleSubject simpleSubject = new SimpleSubject();
SimpleObserver simpleObserver = new SimpleObserver(simpleSubject);
simpleSubject.setValue(80); // output: 80
simpleSubject.removeObserver(simpleObserver);
}
}
xxxxxxxxxx
class JPanelSignal extends JPanel implements Observer {
void init(Signal lSigAObserver) {
lSigAObserver.addObserver(this); // (2) ajout d'observateur
}
void update(Observable observable, Object objectConcerne) {
repaint(); // (3) traitement de l'observation
}
}
The Observer pattern is a behavioral design pattern that defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
In Java, the Observer pattern is implemented using the built-in interfaces Observer and Observable. The Observable class represents the object being observed, while the Observer interface represents the objects that are observing the Observable.
To use the Observer pattern, you first define an Observable object and its corresponding Observer objects. The Observable object maintains a list of its observers, and when its state changes, it calls the notifyObservers() method to notify all its observers of the change. The Observer objects implement the update() method, which is called by the Observable object to update the observer with the new state.
For example, suppose you have a Stock class that represents a stock in a stock market. You could define an Observable subclass called StockMarket that maintains a list of Stock objects and notifies its observers when the prices of these stocks change.
You could then define an Observer interface called StockObserver that defines the update() method, which is called by the StockMarket object to update the observer with the new stock prices. Finally, you could define a Trader class that implements the StockObserver interface and receives notifications from the StockMarket object when the prices of the stocks it is interested in change.