docs/DesignPatterns
view code/observer-example.java @ 33:97b57d24fd7b
added sample code; modified summary
author | meillo@marmaro.de |
---|---|
date | Fri, 10 Aug 2007 22:15:51 +0200 |
parents | |
children | b2cefbd90180 |
line source
1 import java.util.Observable;
2 import java.util.Observer;
4 public class MessageBoard extends Observable {
5 private String message;
7 public String getMessage() {
8 return message;
9 }
11 public void changeMessage(String message) {
12 this.message = message;
13 setChanged();
14 notifyObservers(message);
15 }
17 public static void main(String[] args) {
18 MessageBoard board = new MessageBoard();
19 Student bob = new Student();
20 Student joe = new Student();
21 board.addObserver(bob);
22 board.addObserver(joe);
23 board.changeMessage("More Homework!");
24 }
25 }
27 class Student implements Observer {
28 public void update(Observable o, Object arg) {
29 System.out.println("Message board changed: " + arg);
30 }
31 }