Send the code first, and have time to write the content.
Observer1
1 ImportJava.util.Observer;2 Importjava.util.Observable;3 4 //Student Class (Student) can be observed after inheriting observable5 classStudentextendsobservable{6 //Properties7 PrivateString name;8 Private BooleanIssleep =false;9 Private BooleanIslate =false;Ten //constructor Function One PublicStudent () {} A PublicStudent (String name) { - This. Name =name; - } the //Sleep Behavior - Public voidsleep () { -SYSTEM.OUT.PRINTLN (name + "is sleeping"); -Issleep =true; + setchanged (); - notifyobservers (); + A } at PublicString toString () { - returnname; - } - } - //Teacher Class (Teacher) implements observer interface becomes observer - classTeacherImplementsobserver{ in @Override - Public voidUpdate (Observable o,object arg) { toSystem.out.println ("Teacher:" +o+ ", wakeup!wakeup!"); + } - } the //class Teacher (CLASSHEAD) realizes observer interface becomes observer * classClassheadImplementsobserver{ $ @OverridePanax Notoginseng Public voidUpdate (Observable o,object arg) { -System.out.println ("Classhead:" +o+ ", Cleantheroomafterschool"); the } + } A Public classobserverdemo{ the Public Static voidMain (string[] arguments) { +Student Tom =NewStudent ("Tom"); -Tom.addobserver (Newclasshead ()); $Tom.addobserver (NewTeacher ()); $ -System.out.println ("with" +tom.countobservers () + "Observer in the observation of Tom"); - tom.sleep (); the - }Wuyi}
Observer2
ImportJava.util.Map;ImportJava.util.HashMap;ImportJava.util.Observer;Importjava.util.Observable;//Student Class (Student) can be observed after inheriting observableclassStudentextendsobservable{//Properties PrivateString name; Private BooleanIssleep =false; Private BooleanIslate =false; //constructor Function PublicStudent () {} PublicStudent (String name) { This. Name =name; } //Sleep Behavior Public voidsleep () {System.out.println (name+ "is sleeping"); Issleep=true; Setchanged (); Notifyobservers ("Sleep"); } //Late Behavior Public voidlate () {SYSTEM.OUT.PRINTLN (name+ "was lated"); Islate=true; Setchanged (); Notifyobservers ("Late"); } PublicString toString () {returnname; }}//Teacher Class (Teacher) implements observer interface becomes observerclassTeacherImplementsobserver{//parameter arg as key, the response triggered by this parameter is stored in the map as value Privatemap<string,string> info =NewHashmap<string,string>(); PublicTeacher () {Info.put ("Sleep", ", wakeup!wakeup!"); Info.put ("Late", ", come in."); } //to add a key-value pair Public BooleanAddsay (String k,string v) {returnInfo.put (k,v) = =NULL?false:true; } @Override Public voidUpdate (Observable o,object Arg) {String say=Info.get (ARG); if(Say = =NULL){ return; } System.out.println ("Teacher:" +o+say); }}//class Teacher (CLASSHEAD) realizes observer interface becomes observerclassClassheadImplementsobserver{Privatemap<string,string> info =NewHashmap<string,string>(); PublicClasshead () {Info.put ("Sleep", ", Gooutwashbaface"); Info.put ("Late", ", cleantheroom!"); } Public BooleanAddsay (String k,string v) {returnInfo.put (k,v) = =NULL?false:true; } @Override Public voidUpdate (Observable o,object Arg) {String say=Info.get (ARG); if(Say = =NULL){ return; } System.out.println ("Classhead:" +o+say); }} Public classobserverdemo{ Public Static voidMain (string[] arguments) {Student Tom=NewStudent ("Tom"); Tom.addobserver (Newclasshead ()); Tom.addobserver (NewTeacher ()); System.out.println ("with" +tom.countobservers () + "observers watching Tom"); Tom.sleep (); Tom.late (); }}
Design Pattern 01 Observer pattern (Java)