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