Java Observer Viewer Pattern Program Summary

Source: Internet
Author: User

The Observer pattern is the behavior pattern of the object, also called the Publish-subscribe (publish/subscribe) mode, the Model-view (Model/view) mode, the source-listener (Source/listener) mode, or the slave (dependents) mode.

The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time. When the subject object changes in state, all observer objects are notified so that they can automatically update themselves.
  
public class:

 PackageCom.observer;ImportJava.io.IOException;ImportJava.util.ArrayList;ImportJava.util.List;ImportJava.util.Properties; Public  class maintest {     Public Static void Main(string[] args) {Child c =NewChild (); Properties props =NewProperties ();Try{Props.load (MainTest.class.getClassLoader (). getResourceAsStream ("Observer.properties")); }Catch(IOException e)        {E.printstacktrace (); } String []observers = Props.getproperty ("observers"). Split (","); for(String s:observers) {Try{C.addwakenuplistener (Wakenuplistener) class.forname (s). newinstance ()); }Catch(Instantiationexception e)            {E.printstacktrace (); }Catch(Illegalaccessexception e)            {E.printstacktrace (); }Catch(ClassNotFoundException e)            {E.printstacktrace (); }        }NewThread (c). Start (); }}

Abstract Theme Role Classes

 PackageCom.observer;ImportJava.util.ArrayList;ImportJava.util.List;/** * A Subject object, when the subject object state changes, all observers are notified * @author Iron_man * */Class Child implements Runnable {/** * Save references to all observer objects in List * /    PrivateList<wakenuplistener> wakenuplisteners =NewArraylist<wakenuplistener> ();/** * SAVE Add Observer Object * @param wakenuplistener */     Public void Addwakenuplistener(Wakenuplistener Wakenuplistener)    {Wakenuplisteners.add (Wakenuplistener); }/** * Notifies all Observer objects * /    voidWakeUp () {System.out.println ("Wake"); for(inti =0; I < wakenuplisteners.size ();            i++) {Wakenuplistener Wakenuplistener = Wakenuplisteners.get (i); Wakenuplistener.actiontowakenup (NewWakenupevent (System.currenttimemillis (),"a", This)); }    }@Override     Public void Run() {Try{Thread.Sleep ( the); }Catch(Interruptedexception e)        {E.printstacktrace (); } This. WakeUp (); }}

Abstract Observer Role Class
 

package com.observer;publicinterface WakenUpListener {    publicvoidactionToWakenUp(WakenUpEvent wakeUpEvent);}

Specific Observer Role classes
 

package  Com.observer;/** * Observer Dad, can automatically update himself * @author  Iron_man * */ class Dad Implements Wakenuplistener {public  void  actiontowakenup  (wakenupevent wakenupevent) {System.out.println (" "Feed" ); }}
package com.observer;/** * 观察者GrandFather * @author IRON_MAN * */class GrandFather implements WakenUpListener {    publicvoidactionToWakenUp(WakenUpEvent wakeUpEvent) {        System.out.println("hug");    }}

Abstract event class

 PackageCom.observer;class Wakenupevent {Private LongTimePrivateString Loc;PrivateChild source; Public wakenupevent(LongTime, String Loc, child source) {Super(); This. Time = time; This. loc = loc; This. Source = Source; } Public Long GetTime() {returnTime } PublicStringGetloc() {returnLoc } PublicChildGetSource() {returnSource } Public void settime(LongTime) { This. Time = time; } Public void Setloc(String Loc) { This. loc = loc; } Public void SetSource(Child source) { This. Source = Source; }}

Configuring the Properties File

observers=com.observer.Dad,com.observer.GrandFather

Java Observer Viewer Pattern Program Summary

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.