Observer pattern vs. Java delegate
- The so-called Observer pattern , refers to the need to trigger a series of related actions at the same time, it exists the observer and the notifier two concepts, the notifier holds the observer's abstract class collection, may call all observer's specific method to update the state, At the same time, the observer can hold the abstract object of the notifier to obtain the specific information of the particular notifier.
- The Observer pattern is insufficient : Although the Observer pattern extracts the abstract observer and the notifier, so that the class and the class do not depend on each other, the common dependence on the abstract interface, in line with the dependency reversal, but he is dependent on the abstract interface, and sometimes observers can not inherit abstract observers (such as the reference Jar package).
- The Java delegation mechanism and the observer pattern : the implementation of the delegation mechanism no longer requires the observer abstract class, the observer and the notifier do not depend on each other. Java is implemented using reflection, and the code example is as follows:
Event class
Packagecom.suski.delegate;ImportJava.lang.reflect.Method; Public classEvent {PrivateObject object; PrivateString MethodName; Privateobject[] params; Privateclass[] paramtypes; PublicEvent (Object object,string method,object...args) { This. Object =object; This. MethodName =method; This. params =args; Contractparamtypes ( This. params); } Private voidcontractparamtypes (object[] params) { This. Paramtypes =NewClass[params.length]; for(inti=0;i<params.length;i++) { This. paramtypes[i] =Params[i].getclass (); } } Public voidInvoke ()throwsException {Method Method= Object.getclass (). GetMethod ( This. MethodName, This. paramtypes);//determine if this function exists if(NULL==method) { return; } method.invoke ( This. Object, This. params);//invoking functions with reflection mechanisms }}
Event Management Classes
Packagecom.suski.delegate;Importjava.util.ArrayList;Importjava.util.List; Public classEventHandler {PrivateList<event>objects; PublicEventHandler () {Objects=NewArraylist<event>(); } Public voidaddevent (Object object, String methodName, Object...args) {Objects.add (NewEvent (object, MethodName, args)); } Public voidNotifyx ()throwsException { for(Event event:objects) {event.invoke (); } }}
Notifier Abstract class
Packagecom.suski.delegate; Public Abstract classNotifier {PrivateEventHandler EventHandler =NewEventHandler (); PublicEventHandler Geteventhandler () {returnEventHandler; } Public voidsetEventHandler (EventHandler EventHandler) { This. EventHandler =EventHandler; } Public Abstract voidAddListener (Object object,string methodName, Object...args); Public Abstract voidNotifyx ();}
Notifier Specific Implementation class
Packagecom.suski.delegate; Public classConcretenotifierextendsnotifier{@Override Public voidAddListener (Object object, String methodName, Object ... args) { This. Geteventhandler (). Addevent (object, MethodName, args); } @Override Public voidNotifyx () {Try { This. Geteventhandler (). Notifyx (); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); } }}
Specific observer classes, no longer require abstract observers
Packagecom.suski.delegate;Importjava.util.Date; Public classWatchingtvlistener { PublicWatchingtvlistener () {System.out.println ("Watching TV"); } Public voidSTOPWATCHINGTV (date date) {System.out.println ("Stop watching" +date); }} Packagecom.suski.delegate;Importjava.util.Date; Public classPlayinggamelistener { PublicPlayinggamelistener () {System.out.println ("Playing"); } Public voidstopplayinggame (date date) {System.out.println ("Stop playing" +date); }}
Test method
Packagecom.suski.delegate;Importjava.util.Date; Public classTest { Public Static voidMain (string[] args) {Notifier goodnotifier=NewConcretenotifier (); Playinggamelistener Playinggamelistener=NewPlayinggamelistener (); Watchingtvlistener Watchingtvlistener=NewWatchingtvlistener (); Goodnotifier.addlistener (Playinggamelistener,"Stopplayinggame",NewDate ()); Goodnotifier.addlistener (Watchingtvlistener,"Stopwatchingtv",NewDate ()); Goodnotifier.notifyx (); }}
Observer patterns and Java delegates