Design Patterns observer mode

The observer pattern 

defined observers (Observer) mode:
means exists among the plurality of objects -to-many dependencies, when a state of the object changes,
all objects that depend on it have been notified and updated automatically .
This mode is sometimes referred to publish - subscribe model, model - view mode , it is the object behavioral patterns.

Abstract topics (Subject) Role:
also known as an abstract object classes, which provides aggregation and increase a class for holding observer objects, delete objects observer method,
and notify all observers of abstract methods.
Specific topics (Concrete Subject) Role :
also known as specific target class that implements the abstract goal of the notification method,
when the internal state specific theme changes, notify all registered observers object.
Abstract observer (Observer) role:
it is an abstract class or interface, which includes an updated his abstract method, when receiving change notification specific theme is called.
Specific observer (Concrete Observer) roles:
the abstract methods defined in the abstract observer implemented in order to update their status when obtaining change notification goals.
. 1  Import of java.util.ArrayList;
 2  Import java.util.List;
 . 3  
. 4  public  class the Observer {
 . 5      public  static  void main (String [] args) {
 . 6          Subjecting Sub = new new ConcreteSubject ();
 . 7          sub.add ( new new Observering1 ());
 . 8          sub.add ( new new Observering2 ());
 . 9          ((ConcreteSubject) Sub) .change ( "la makeover" );
 10      }
 11  }
 12 is  
13 is  // first is a parent of the observer, it maintains a collection of references to functions fitted observer.
14 abstract class Subjecting {
15     //观察者集合
16     private List<Observering> arr = new ArrayList<Observering>();
17 
18     public void add(Observering ob) {
19         arr.add(ob);
20     }
21 
22     public void remove(Observering ob) {
23         arr.remove(ob);
24     }
25 
26     //通知观察者
27     public void update(String newstatue) {
28         for (int i = 0; i < arr.size(); i++) {
29             Observering observering = arr.get(i);
30             observering.update(newstatue);
31         }
32     }
33 }
34 
35 //具体的被观察者对象
36 class ConcreteSubject extends Subjecting {
37     private String statue;
38 
39     public String getStatue() {
40         return statue;
41     }
42 
43     public void setStatue(String statue) {
44         this.statue = statue;
45     }
46 
47     public void change(String statuee) {
48         statue = statuee;
49         update(statuee);
50     }
51 }
52 
53 //观察者
54 interface Observering {
55     public void update(String statue);
56 }
57 
58 class Observering1 implements Observering {
59     private String statue;
60 
61     @Override
62     public void update(String statuee) {
63         statue = statuee;
64         System.out.println(statuee + "---快走");
65     }
66 }
67 
68 class Observering2 implements Observering {
69     private String statue;
70 
71     @Override
72     public void update(String statuee) {
73         statue = statuee;
74         System.out.println(statuee + "--快----跑---");
75     }
76 }

Guess you like

Origin www.cnblogs.com/loveer/p/11279698.html