The main research in this article is the implementation of Java callback functions and observer patterns. The specific introduction and implementation code are as follows.
Observer mode (sometimes called publish-subscribe mode, model-view mode, source-listener mode or slave mode) is a type of software design mode. In this mode, a target object manages all observer objects that depend on it and actively issues notifications when its state changes. This is usually achieved by calling each observer's methods. This pattern is usually used to implement event processing systems.
In fact, the observer model has a common use environment with the bridges and strategies mentioned above: independently encapsulate changes to achieve maximum reuse and decoupling. The difference between the observer and the latter two is that the target in the observer pattern and the change in the observer are not independent, but have some connection.
Observer mode is implemented in Java through the Observable class and the Observer interface. An Observer object monitors the changes of an Observable object. When the Observable object changes, the Observer is notified and can perform corresponding work.
package com.demo.test;import java.util.Observable;import java.util.Observer;//In the observer mode, the target class maintains all observers' references, while the callback only maintains a reference public class ObserverCallbackDemo {// ObserverA Astatic class ConcreteObserverA implements Observer {@Override public void update(Observable o, Object arg) {System.out.println("ConcreteObserverA update");}}// Observer Bstatic class ConcreteObserverB implements Observer {@Override public void update(Observable o, Object arg) {System.out.println("ConcreteObserverB update");}}// Observed object static class ConcreteObservable extends Observable {public void changeValue() {//protected method can only be called in subclass setChanged();notifyObservers();}}// Callback function interface interface ICallback {public void onCall();}// Callback class static class CallbackDemo {private ICallback callback;public void setListener(ICallback callback) {this.callback = callback;}public void call() {callback.onCall();}}public static void main(String[] args) {// ObserverConcreteObserverA observerA = new ConcreteObserverA();ConcreteObserverB observerB = new ConcreteObserverB();ConcreteObservable observable = new ConcreteObservable();observable.addObserver(observerA);observable.addObserver(observerB);System.out.println("countObservers = " + observable.countObservers());observable.changeValue();// Callback function CallbackDemo callbackDemo = new CallbackDemo();callbackDemo.setListener(new ICallback() {@Override public void onCall() {System.out.println("callback onCall");}});callbackDemo.call();}}Output result:
countObservers = 2
ConcreteObserverB update
ConcreteObserverA update
callback onCall
From the above code, we can see that the callback function should belong to the observer pattern, and the purpose is to replace the round-robin mechanism and reduce the coupling between components. In the observer mode, the target class maintains all observers' references, while the callback only maintains a reference.
The above is all about Java callback functions and observer pattern instance code in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!