Java callback function and observer mode instance code, callback function observer

Source: Internet
Author: User

Java callback function and observer mode instance code, callback function observer

This article focuses on the implementation of Java callback functions and observer mode. The specific introduction and implementation code are as follows.

Observer mode (sometimes referred to as publish-Subscribe mode, Model-View Mode, source-Listener mode or subordinate Mode) is a software design model.In this mode, a target object manages all observer objects that depend on it and actively sends notifications when its status changes. This is usually done by calling the methods provided by various observers. This mode is usually used to implement the event processing system.

When to use the observer mode:
  1. When an abstract model has two aspects, one of which depends on the other. Encapsulate the two in independent objects so that they can be changed and reused independently.
  2. When changing an object, you need to change other objects at the same time without knowing how many objects need to be changed.
  3. When an object must notify other objects, it cannot assume who the other objects are. In other words, you do not want these objects to be tightly coupled.

In fact, the observer mode shares a common use environment with the bridges and strategies mentioned above: changes are encapsulated independently to achieve maximum reuse and decoupling. The difference between the observer and the latter is that the changes of the target and observer in the Observer mode are not independent, but have some relationships.

In Java, the Observer mode is implemented 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 will be notified to perform relevant work.

Package com. demo. test; import java. util. observable; import java. util. observer; // The object class in the Observer mode maintains reference of all observers, the callback only maintains a reference to public class ObserverCallbackDemo {// Observer 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");} // The observed object static class ConcreteObservable extends Observable {public void changeValue () {// The protected method can only be called in the subclass setChanged (); notifyObservers () ;}/// callback function 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) {// 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 (); // 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

Summary

From the code above, we can see that the callback function should belong to the observer mode, in order to replace the round robin mechanism and reduce the coupling between components. In the observer mode, the target class maintains reference of all observers, while the callback only maintains a reference.

The above is all the content of the Java callback function and the observer mode instance code in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.