Callback function and listener mode of notes

A callback function
callback function is a functional fragment of a, by the user according to the calling conventions implemented a callback function. There is such a popular definition: programmer A wrote a program (program a), which reserved a callback function interface, and encapsulated the program. B programmers make a call to a method for their application in b, then, a call to the interface through his own Method b is
an example:
There are two entities: abstract interface callback, the callback person (i.e., a program)
Callback interface (ICallBack)
public interface ICallBack {
public void callBack();
}
Callback (the class used to call the callback function)
public class Caller{
public void call(ICallBack callBack) {
System.out.println("start... ");
callBack.callBack();
System.out.println("end...");
}
}
Callback test:
public static void main(String[] args) {
        Caller call = new Caller();
        call.call (new ICallBack(){


        @Override
        public void callBack() {
            System.out.println("The callback is finally successful!");
       
        });


}The
console output:
start...The


callback is finally successful!


end...
2. Event monitoring mode
Java's event monitoring mechanism can be summarized as three points:
1. Java's event monitoring mechanism involves three components : event source, event listener, and event object. The listener is generally an interface. Convention call method.
2. When an operation occurs on the event source object, it will call a method of the event listener, and pass the event object when the method is called.
3. The event listener implementation class is usually written by the developer. The developer gets the event source through the event object, and then processes the operation on the event source.
Listener interface
public interface EventListener extends java.util.EventListener {
    //Event handling
    public void handleEvent(EventObject event);
}
Event object
public class EventObject extends java.util.EventObject{
    private static final long serialVersionUID = 1L;
    public EventObject(Object source){
        super(source);
    }
    public void doEvent(){
        System.out.println("Notify an event source source:"+ this.getSource());
    }


}
Event source
public class EventSource {
    / /Lister list, the registration of the listener will be added to this list
    private Vector<EventListener> ListenerList = new Vector<EventListener>();
    //Register the listener
    public void addListener(EventListener eventListener){
        ListenerList.add(eventListener);
    }
    / /Unregister
    public void removeListener(EventListener eventListener){
        ListenerList.remove(eventListener);
    }
  //Accept external events
    public void notifyListenerEvents(EventObject event){        
        for(EventListener eventListener:ListenerList){
                eventListener.handleEvent(event);
        }
    }


}
Here to understand: The custom listener implementation class implements the java.util.listener interface and provides its interface method handleEvent (EventObject event). The implementation is basically correct Event object processing
   Custom event classes implement the java.util.EventObject interface. The creation of event objects requires the event source to also include the specific behavior of the
   event. The event source needs to be defined in the specific business class. The listener list needs to be defined. The listener is added and deleted, and the listener is defined. The notification method
zop listener understands: Define the listener interface zoplistener inherits the java.util.listener interface (it is characteristic and does not have any meaning), defines two listeners LoggerListener and MassageListener and implements the handlerEvent (ZopEvent event) method
(ZopEvent is in Based on the EventObject class, the properties are extended. In addition to the Object source, there are the event type String eventType (including login, query...) and the transmission object Object data).
Here is to monitor the user's operation of the system and generate the operation. Log. At the same time define the listener support class ZopListenerSupport which provides a list of listeners to define the methods added by the listener and the notification method that triggers the event (in fact, it is for each
Listeners call an event handler for a listener's actually a list traversal and calling each listener's handleEvent ()), here at the same time define a listener class configuration by configuration and bean notes
the listener supports the creation of cross-class ZoplistenerSupport Do it for the bean container and manage it (the creation of the support class here is also an initialization of its listener list).

Guess you like

Origin blog.csdn.net/u011445756/article/details/80174602