Java callback mechanism and observer pattern instance sharing

Source: Internet
Author: User

Callback functions are used frequently in Java, such as the listener events in swing visual coding, and so on, the general callback function is the callback method that calls the callback object automatically through the execution of a method, for example, there is an interface, there is a method onnotify (), this method is a callback method, Indicates the method of the call when a trigger event occurs, for example:

A callback interface

/* * *  @author Minliangzhi *  * */publicinterface  icallback {         void onnotify ();}

A concrete entity object that starts the execution of the callback function:

/**  *   @author Minliangzhi *   */  Public class Task {    private  icallback callBack;          Public Task (Icallback callBack) {        this. callBack = callBack;    }           Public void dosomething () {        callback.onnotify ();    }}

The specific use method is:

     Public Static void Main (string[] args) {        new Task (new  icallback () {                        @Override              publicvoid  onnotify () {                System.out.println ("I am the callback method");}        ). DoSomething ();    }

By invoking the method of the entity object, the callback method of the callback function object is triggered, and we simply provide a callback object using the method of an anonymous class.

Thus, we can analyze the specific meanings and general implementations of the following observer patterns:

The observer pattern can be divided into two parts, one for the publisher, one for the observer, and some for the publisher, to notify all observers and let them know the publisher's release information.

Observed by:

/** *  @authorMinliangzhi * @date September 8, 2016*/ Public InterfaceObserver {voidonnotify (String content);} Public classMikeImplementsObserver {@Override Public voidonnotify (String content) {System.out.println ("Mike Get:" +content); }} Public classBobImplementsObserver {@Override Public voidonnotify (String content) {System.out.println ("Bob Get:" +content); }}

The above defines two observers, and the following defines a post-message person:

/** *  @authorMinliangzhi * @date September 8, 2016*/ Public InterfacePublisher {/*** Register an observer *@paramObserver*/    voidregistobserver (Observer Observer); /*** Remove a viewer *@paramObserver*/    voidremoveobserver (Observer Observer); /*** Notify all observers*/    voidnotifyallobserver ();}

A specific implementation class, the work publisher Hunter:

/** *  @authorMinliangzhi * @date September 8, 2016*/ Public classHunterImplementsPublisher {PrivateList<observer>Observer; PrivateString jobcontent;  Public voidsetjob (String content) { This. jobcontent =content; }         PublicHunter () { This. Observer =NewArraylist<observer>(); } @Override Public voidregistobserver (Observer Observer) { This. OBSERVER.ADD (Observer); } @Override Public voidremoveobserver (Observer Observer) { This. OBSERVER.REMOVE (Observer); } @Override Public voidNotifyallobserver () { for(Observer ob:observer) {ob.onnotify (jobcontent); }    }}

How to use:

     Public Static voidMain (string[] args) {/**construct publishers and observers, and register*/Hunter Hunter=NewHunter (); Observer Bob=NewBob (); Observer Mike=NewMike ();        Hunter.registobserver (Bob);        Hunter.registobserver (Mike); /**publish a message informing all observers*/Hunter.setjob ("JAVA");    Hunter.notifyallobserver (); }

Java callback mechanism and observer pattern instance sharing

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.