Design Pattern 01 Observer pattern (Java)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.