Design Pattern 01 Observer pattern (Java)

Source: Internet
Author: User

1 ImportJava.util.Map;2 ImportJava.util.HashMap;3 ImportJava.util.Observer;4 Importjava.util.Observable;5 6 //Student Class (Student) can be observed after inheriting observable7 classStudentextendsobservable{8     //Properties9     PrivateString name;Ten     Private BooleanIssleep =false; One     Private BooleanIslate =false; A     //constructor Function -      PublicStudent () {} -      PublicStudent (String name) { the          This. Name =name; -     } -     //Sleep Behavior -      Public voidsleep () { +SYSTEM.OUT.PRINTLN (name + "is sleeping"); -Issleep =true; + setchanged (); ANotifyobservers ("Sleep"); at          -     } -     //Late Behavior -      Public voidlate () { -SYSTEM.OUT.PRINTLN (name + "was lated"); -Islate =true; in setchanged (); -Notifyobservers ("Late"); to     } + } - //Teacher Class (Teacher) implements observer interface becomes observer the classTeacherImplementsobserver{ *     //parameter arg as key, the response triggered by this parameter is stored in the map as value $     Privatemap<string,string> info =NewHashmap<string,string>();Panax Notoginseng      PublicTeacher () { -Info.put ("Sleep", "wakeup!wakeup!"); theInfo.put ("Late", "Come in."); +     } A     //to add a key-value pair the      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){ the                 return; -             }WuyiSystem.out.println ("Teacher:" +say); the     } - } Wu //class Teacher (CLASSHEAD) realizes observer interface becomes observer - classClassheadImplementsobserver{ About     Privatemap<string,string> info =NewHashmap<string,string>(); $      PublicClasshead () { -Info.put ("Sleep", "Gooutwashbaface"); -Info.put ("Late", "cleantheroom!"); -     } A      Public BooleanAddsay (String k,string v) { +         returnInfo.put (k,v) = =NULL?false:true; the     } - @Override $      Public voidUpdate (Observable o,object arg) { theString say =Info.get (ARG); the             if(Say = =NULL){ the                 return; the             } -System.out.println ("Classhead:" +say); in     } the } the  Public classobserverdemo{ About      Public Static voidMain (string[] arguments) { theStudent Tom =NewStudent ("Tom"); theTom.addobserver (Newclasshead ()); theTom.addobserver (NewTeacher ()); +  -System.out.println ("with" +tom.countobservers () + "Observer in the observation of Tom"); the tom.sleep ();Bayi tom.late (); the     } the}

Design Pattern 01 Observer pattern (Java)

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.