Java implementation callbacks (implementations of various listener in android,swing)

Source: Internet
Author: User

?????? Developers familiar with the ms-windows and X Windows Event-driven design patterns typically pass a pointer to a method to the event source, calling this method (also called a "callback") when an event occurs. The Java object-oriented model does not currently support method pointers and does not seem to be able to use this handy mechanism.
????? Java supports interface, and the same callbacks can be implemented through interface. The trick is to define a simple interface that declares a method that is expected to be recalled.
????? For example, suppose you are notified when an event occurs, we can define a interface:
?????????? Public interface Interestingevent {
????????????? This is just an ordinary method, you can receive parameters, you can return a value
????????????? public void interestingevent ();
??????????}
So that we have any one implementation of this interface class object handle grip.

When an event occurs, the object that implements the Interestingevent interface needs to be notified and the Interestingevent () method is called.
Class Eventnotifier {
??? Private interestingevent ie;
??? Private Boolean somethinghappened;
??? Public? Eventnotifier(Interestingevent event) {
??????? ie = event;
??????? somethinghappened = false;
??? }
??? public void? doWork() {
??????? if (somethinghappened) {
??????????? When an event occurs, this method of calling the interface is used to notify
??????????? Ie.interestingevent ();
??????? }???????
??? }
}
In this example, somethinghappened is used to flag whether the event occurred.

The class that wants to receive the event notification must implement the Interestingevent interface and pass its own reference to the notifier of the event.
public class CallMe implements Interestingevent {
??? Private Eventnotifier en;
??? Public CallMe () {
??????? Create a new event Notifier object and pass yourself to it
??????? En = new Eventnotifier (this);
??? }
??? The method that actually handles the event when the implementation event occurs
??? public void Interestingevent () {
??????? This incident happened, handled.
??? }
}
The above is a very simple example to illustrate the implementation of callbacks in Java.
Of course, you can also register multiple objects that are interested in this event in the event management or event Notifier class. 1. Define an interface Interestingevent, callback method Nterestingevent (string event) simply receives a string parameter.??????????? Interface Interestingevent {???????? public void Interestingevent (String event);
?????}
2. Implement Interestingevent interface, event processing class????? Class CallMe implements Interestingevent {
??? ????? private String name;
???????? Public CallMe (String name) {
????? this.name = name;
??? ?????}???
??? ????? public void Interestingevent (String event) {
??????? ????? SYSTEM.OUT.PRINTLN (name + ": [" +event? + "] happened");
???????? }
?????}

3. Event Manager, or event notifier
????? Class Eventnotifier {
??? ????? Private list<callme> callmes = new arraylist<callme> ();
??? ????? public void Regist (CallMe CallMe) {
??????? ????? Callmes.add (callMe);
??? ?????}
???
???????? public void DoWork () {
???????????? for (CallMe callme:callmes) {
???????????????? Callme.interestingevent ("Sample Event");
??????? ?????}
??? ?????}???
?????}

4. Testing
????? public class Callmetest {
???????? public static void Main (string[] args) {
???????????? Eventnotifier ren = new Eventnotifier ();
??????? ????? CallMe a = new CallMe ("CallMe a");
???????????? CallMe B = new CallMe ("CallMe B");

???????????? Regiest
???????????? Ren.regist (a);
???????????? Ren.regist (b);
???????
???????????? Test
???????????? Ren.dowork ();???????
??? ?????}
?????}

Java implementation callbacks (implementations of various listener in android,swing)

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.