docs/DesignPatterns
view code/observer-example.java @ 36:f03413250b39
some typos and cleanups
author | meillo@marmaro.de |
---|---|
date | Sat, 11 Aug 2007 22:42:24 +0200 |
parents | 97b57d24fd7b |
children |
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 }