The difference between template mode and method callback and observer mode

Template mode

Refer to https://blog.csdn.net/ezconn/article/details/108812616

Observer mode

Reference https://blog.csdn.net/ezconn/article/details/108564814

Method callback or callback function

Callback is a two-way calling relationship. A Class H a function registered in advance to Class B, Class A call M class B function time, which in turn calls B Class A Class H functions registered to it . The H function here is the "callback function". A calls B, and B in turn calls A. This calling mechanism is called "callback".

Expressed by Java code

public interface ICallback {
    void onCallback();
}

public class BClass {
    public void setCallback(ICallback callback) {
        //...
        callback.onCallback();
        //...
    }
}

public class AClass {
    public static void main(String[] args) {
        BClass b = new BClass();
        b.setCallback(new ICallback() { //回调对象
            @Override
            public void onCallback() {//回调方法
                System.out.println("Call back me.");
            }
        });
    }
}

Synchronous callback and asynchronous callback

Callbacks can be divided into synchronous callbacks and asynchronous callbacks (or delayed callbacks). Synchronous callback refers to the execution of the callback function before the function returns; asynchronous callback refers to the execution of the callback function after the function returns. The above code is actually the implementation of synchronous callback. The callback function onCallback() is executed before the setCallback() function returns. An example of asynchronous callback: the payment function is realized through the third-party payment system. After the user initiates a payment request, the user generally does not block until the payment result is returned, but registers the callback interface to the third-party payment system. After the third-party payment system is executed, the The result is returned to the caller through the callback interface.

Asynchronous callback

Monitoring click events in Android must have been used; interaction is generally inseparable from event monitoring;

new Button(this).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("点击了按钮");
            }
});

From the point of view of the code structure, the event listener is like a callback, that is, an object containing a callback function (onClick()) is passed to another function. From the application scenario, it is very similar to the observer mode, that is, the observer (OnClickListener) is registered in advance, when the user clicks the button, the click event is sent to the observer, and the corresponding onClick() function is executed. The callback here is Asynchronous callback, after we register the callback function in the setOnClickListener() function, we don't need to wait for the callback function to execute.

From the perspective of application scenarios, synchronous callbacks look more like template mode, and asynchronous callbacks look more like observer mode.

From the point of view of code implementation, callback and template mode are completely different. Callback is implemented based on composition relationship. Passing one object to another is a relationship between objects; template mode is implemented based on inheritance relationship. Subclass overrides the abstract method of parent class, which is a relationship between classes. .

 

Guess you like

Origin blog.csdn.net/ezconn/article/details/108817097
Recommended