For a lot of things. All are constantly changing. That is to say. In constant changes. We must get it
The change between the objects associated with each other. That is, other objects can be notified. But different from the situation
Because after all, it is only a change in the corresponding value to tell each other. Sentence is an object
The state changes. A set of objects corresponding to it will change. This will lead to the observer. Notice
The correspondence. The observer discovers changes. The notice tells each other to change the object.
This is the so-called OBServer mode. As usual, we start from abstract objects
The following is a case. Suppose you are a primary school. Your results have increased by 10 points. Your zero money increases
5%. And parents increase your care 3. The opposite is reduced. (This is just a bit more.)
// First construct abstract class Abstract Class Student {public string sname; public int loveeLevel; public int
Portected IOBSERVER OB;
Public Student (String N, INT G, INT L, INT M) {SNAME = N; grade = g; loveelevel = L; Money = m;}
Public void addobserver (IOBSERVER O) {this.ob = o; this.ob.obgrade = this.grade;} public void removeobserver () {this.ob = null this.ob.obgrade = -1;} public void notify ) {Ob.update ();
}
// Realize the student object (actually for expanding this) PUBLIC CLASS MyStudent: Student {// If you want to join what you want Public MyStudent (String S, INT G, INT L, INT M) {base (s, g, l, m);}}
Below we are to construct Observer objects. What should I do with OBSERVER objects? Obviously. Just
Whether the student's score has changed. And make the corresponding treatment.
Interface IOBSERVER {stadent student {get; set;}; void update ();
/ / Implement Observer object
public class StudentObserver: IObserver {private Student s; public int ObGrade public Student student {get {return s;} set {s = value;}} public void Update () {if ((s.Grade-this.ObGrade)> = 10) {S.Money = S.money * 0.05; s.loveevel = 3; this.obgrade = s.grade; console.writeLine ("{0}) Due to the number of achievements, the amount of money is: {1} The love index is: {2} ", ssname, s.money, s.loveevel);} else if ((this.obgrade-s.grade> = 10) {s.money = S.money * 0.05; s.loveevel = 3; this.obgrade = s.grade; console.writeline ("{0} Because the number of results is made: {1} Love index is: {2}", s.sname, s.money, s.lovelevel) } Else {this.obgrade = s.grade; console.writeline ("No change") return;}}} // Take a look at the public class client {public static int main (String [] args) {MyStudent S = New M YSTUDENT ("Yarshray", 60, 90, 100); IOBSERVER IO = New StudentobServer (); S.Addobser (IO); s.Notify (); s.grade- = 10; s.Notify (); s.grade = 11 ; S.Notify (); return 0;}}
Once the results have changed, once the results have changed, they immediately notify the observer. At the same time, the observer will make it accordingly. Here if you use ArrayList
It is to observe a group of student objects. Don't do it in terms of aspect. Specific applications