Callback functions are used frequently in Java, such as the listener events in swing visual coding, and so on, the general callback function is the callback method that calls the callback object automatically through the execution of a method, for example, there is an interface, there is a method onnotify (), this method is a callback method, Indicates the method of the call when a trigger event occurs, for example:
A callback interface
/* * * @author Minliangzhi * * */publicinterface icallback { void onnotify ();}
A concrete entity object that starts the execution of the callback function:
/** * @author Minliangzhi * */ Public class Task { private icallback callBack; Public Task (Icallback callBack) { this. callBack = callBack; } Public void dosomething () { callback.onnotify (); }}
The specific use method is:
Public Static void Main (string[] args) { new Task (new icallback () { @Override publicvoid onnotify () { System.out.println ("I am the callback method");} ). DoSomething (); }
By invoking the method of the entity object, the callback method of the callback function object is triggered, and we simply provide a callback object using the method of an anonymous class.
Thus, we can analyze the specific meanings and general implementations of the following observer patterns:
The observer pattern can be divided into two parts, one for the publisher, one for the observer, and some for the publisher, to notify all observers and let them know the publisher's release information.
Observed by:
/** * @authorMinliangzhi * @date September 8, 2016*/ Public InterfaceObserver {voidonnotify (String content);} Public classMikeImplementsObserver {@Override Public voidonnotify (String content) {System.out.println ("Mike Get:" +content); }} Public classBobImplementsObserver {@Override Public voidonnotify (String content) {System.out.println ("Bob Get:" +content); }}
The above defines two observers, and the following defines a post-message person:
/** * @authorMinliangzhi * @date September 8, 2016*/ Public InterfacePublisher {/*** Register an observer *@paramObserver*/ voidregistobserver (Observer Observer); /*** Remove a viewer *@paramObserver*/ voidremoveobserver (Observer Observer); /*** Notify all observers*/ voidnotifyallobserver ();}
A specific implementation class, the work publisher Hunter:
/** * @authorMinliangzhi * @date September 8, 2016*/ Public classHunterImplementsPublisher {PrivateList<observer>Observer; PrivateString jobcontent; Public voidsetjob (String content) { This. jobcontent =content; } PublicHunter () { This. Observer =NewArraylist<observer>(); } @Override Public voidregistobserver (Observer Observer) { This. OBSERVER.ADD (Observer); } @Override Public voidremoveobserver (Observer Observer) { This. OBSERVER.REMOVE (Observer); } @Override Public voidNotifyallobserver () { for(Observer ob:observer) {ob.onnotify (jobcontent); } }}
How to use:
Public Static voidMain (string[] args) {/**construct publishers and observers, and register*/Hunter Hunter=NewHunter (); Observer Bob=NewBob (); Observer Mike=NewMike (); Hunter.registobserver (Bob); Hunter.registobserver (Mike); /**publish a message informing all observers*/Hunter.setjob ("JAVA"); Hunter.notifyallobserver (); }
Java callback mechanism and observer pattern instance sharing