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