docs/DesignPatterns

diff code/observer-example.java @ 34:b2cefbd90180

some redesign; new content for summary and appendix
author meillo@marmaro.de
date Sat, 11 Aug 2007 12:49:00 +0200
parents 97b57d24fd7b
children
line diff
     1.1 --- a/code/observer-example.java	Fri Aug 10 22:15:51 2007 +0200
     1.2 +++ b/code/observer-example.java	Sat Aug 11 12:49:00 2007 +0200
     1.3 @@ -1,31 +1,31 @@
     1.4 -        import java.util.Observable;
     1.5 -        import java.util.Observer;
     1.6 +    import java.util.Observable;
     1.7 +    import java.util.Observer;
     1.8  
     1.9 -        public class MessageBoard extends Observable {
    1.10 -          private String message;
    1.11 +    public class MessageBoard extends Observable {
    1.12 +      private String message;
    1.13  
    1.14 -          public String getMessage() {
    1.15 -            return message;
    1.16 -          }
    1.17 +      public String getMessage() {
    1.18 +        return message;
    1.19 +      }
    1.20  
    1.21 -          public void changeMessage(String message) {
    1.22 -            this.message = message;
    1.23 -            setChanged();
    1.24 -            notifyObservers(message);
    1.25 -          }
    1.26 +      public void changeMessage(String message) {
    1.27 +        this.message = message;
    1.28 +        setChanged();
    1.29 +        notifyObservers(message);
    1.30 +      }
    1.31  
    1.32 -          public static void main(String[] args) {
    1.33 -            MessageBoard board = new MessageBoard();
    1.34 -            Student bob = new Student();
    1.35 -            Student joe = new Student();
    1.36 -            board.addObserver(bob);
    1.37 -            board.addObserver(joe);
    1.38 -            board.changeMessage("More Homework!");
    1.39 -          }
    1.40 -        }
    1.41 +      public static void main(String[] args) {
    1.42 +        MessageBoard board = new MessageBoard();
    1.43 +        Student bob = new Student();
    1.44 +        Student joe = new Student();
    1.45 +        board.addObserver(bob);
    1.46 +        board.addObserver(joe);
    1.47 +        board.changeMessage("More Homework!");
    1.48 +      }
    1.49 +    }
    1.50  
    1.51 -        class Student implements Observer {
    1.52 -          public void update(Observable o, Object arg) {
    1.53 -            System.out.println("Message board changed: " + arg);
    1.54 -          }
    1.55 -        }
    1.56 +    class Student implements Observer {
    1.57 +      public void update(Observable o, Object arg) {
    1.58 +        System.out.println("Message board changed: " + arg);
    1.59 +      }
    1.60 +    }