The Java callback function with the Observer pattern instance code

  • 2021-01-18 06:25:59
  • OfStack

This paper mainly studies the implementation of Java callback function and Observer pattern. The specific introduction and implementation code are as follows.

The Observer pattern (sometimes referred to as Publisher (publish) -Subscribe (Subscribe), Model-View (View), Sailor-Listener (Listener), or Subscriber pattern) is one of the software design patterns. In this mode, a target object manages all dependent observer objects and actively notifies itself when its own state changes. This is usually done by calling the methods provided by each observer. This pattern is often used to implement event processing systems.

When to use Observer mode:

When an abstract model has two aspects, one of which depends on the other. Encapsulate the two in separate objects so that they can be changed and reused independently of each other. When a change to one object requires a change to other objects at the same time, it is not known how many objects need to be changed. When an object must notify other objects, it cannot assume who the other objects are. In other words, you don't want these objects to be tightly coupled.

In fact, the Observer pattern shares the same context as the previous Bridges and strategies: to encapsulate changes independently for maximum reuse and decoupling. The difference between the observer and the latter two is that the change of the target and observer in the observer model is not independent, but has some relationship.

The Observer pattern is implemented in Java through the Observable class and the Observer interface. An Observer object monitors changes in an Observable object, and when Observable object changes, Observer is notified and can work accordingly.


package com.demo.test;
import java.util.Observable;
import java.util.Observer;
// The target class maintains references to all observers in the observer pattern, whereas the callback only maintains references to all observers 1 A reference 
public class ObserverCallbackDemo {
	//  The observer A
	static class ConcreteObserverA implements Observer {
		@Override
		    public void update(Observable o, Object arg) {
			System.out.println("ConcreteObserverA update");
		}
	}
	//  The observer B
	static 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 Methods can only be called in subclasses 
			setChanged();
			notifyObservers();
		}
	}
	//  Callback function interface 
	interface ICallback {
		public void onCall();
	}
	//  The 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) {
		//  The observer 
		ConcreteObserverA 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();
		//  The callback function 
		CallbackDemo callbackDemo = new CallbackDemo();
		callbackDemo.setListener(new ICallback() {
			@Override
			      public void onCall() {
				System.out.println("callback onCall");
			}
		}
		);
		callbackDemo.call();
	}
}

Output results:

[

countObservers = 2
ConcreteObserverB update
ConcreteObserverA update
callback onCall

]

conclusion

As can be seen from the above code, callback functions should be one of the observer patterns, in order to replace the round-robin mechanism and reduce the coupling between components. The target class maintains all observer references in the observer pattern, while the callback maintains only one reference.

That's the end of the code for the Java callback and Observer pattern example. I hope you found it helpful. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!


Related articles: