Realize the callback process in Java

zhaozj2021-02-16  53

Realize the callback process in Java

Using the interface in Java to achieve the same functional callback function

Summary:

The Java interface provides a good way to implement the callback function. If you are accustomed to the programming model of the event-driven programming model, you will like this skill by transferring a function pointer.

Author: John D. Mitchell

In the event drive model of the MS-Windows or X-WINDOW system, when some events occur, the developer is familiar with the processing method by transferring a function pointer. In Java's object-oriented model, this method cannot be supported, and it seems that it seems that this more comfortable mechanism is used, but it is not the case.

Java's interface provides a good mechanism to let us achieve the same effect and callback. This trick is to set a simple interface, define a method we want to call among the interface.

For example, it is assumed that when an event occurs, we think it is notified, then we define an interface:

Public Interface InterestingEvent

{

// this is Just A Regular Method So It Can Return Something OR

// Take Arguments if you like.

Public void interestingEvent ();

}

This gives us a control point for all classes of the interface. Therefore, we don't need to care about any other external type information related to yourself. This method is better than the C function, because in the C style code, you need to specify a data field to save the object pointer, and this implementation in Java is not required.

The class that issues an event requires an object to implement the InterestingEvent interface, and then call the interestingEvent () method in the interface.

Public Class EventNotifier

{

Private InterestingEvent IE;

Private boolean something;

Public EventNotifier (InterestingEvent Event)

{

// Save The Event Object for later.

IE = Event;

// Nothing to report yet.

Somethinghapped = false;

}

// ...

Public void doork ()

{

// Check The Predicate, Which is set elsewhere.

IF (Somethinghapped)

{

// Signal The even by invoking the interface's method.

IE.INTERESTINGEVENT ();

}

// ...

}

// ...

}

In this example, we use the SomethingHapped flag to track whether the event should be excited. In many cases, the modified method can stimulate the interestingEvent () method is correct.

I want to receive an event notification to implement the InterestingEvent interface, and the correct delivery of its own reference to the event notifier.

Public Class Callme IMPLEments INTERESTINGEVENT

{

PRIVATE EVENTNOTIFIER EN;

Public callme ()

{

// Create The Event Notifier and Pass Oursf to IT.

En = New EventNotifier (this);

}

// define the actual handler for the evenet.

Public void interestingEvent () {

// WOW! Something Reali Interesting Must Have Occurred!

// do something ...

}

// ...

}

I hope this tip can bring you convenience.

About the Author:

John D. Mitchell has been a consultant in the past nine years, and has developed PDA software, interest in writing compilers, TCL / TK, and Java systems in GeoWorks using OO assembly language. And people with "Making Sense of Java", currently working in the Java compiler.

转载请注明原文地址:https://www.9cbs.com/read-26508.html

New Post(0)