AD

java implementation callback (transfer)

Abstract Java supports method pointers before, Java interfaces can not provide a good way to achieve the callback. If you are accustomed to pass in the event-driven programming model to call a function pointer, then you'll like this technique.
Are familiar with MS-Windows and X Window System event-driven programming model for developers who are accustomed to pass in a certain time of the incident call (that is, "callback"), a function pointer. Java, object-oriented model currently does not support method pointers, which would be impossible to use this very good mechanism. But we are not that nothing can be done!
Java provides an interface to support access to the equivalent function callback mechanism. Their skills are: the definition of a simple interface, and declared in the interface we have to call the method.

For example, assume that we want to be notified when an event occurs. We can define an interface:

public interface InterestingEvent (
/ / This is just a routine method. Therefore, if necessary,
/ / It can have return values, but also can receive parameters.
public void interestingEvent ();
)

This allows us to implement this interface to control any object class. Therefore, we do not care about any external type information. And in the C + + code for the Motif widgets use the data fields to accommodate the object pointer of the C function is difficult to control, this method is much better.

Given the event a class must wait for the signal to achieve the InterestingEvent interface object and at the appropriate time called interestingEvent () method.

public class EventNotifier (
private InterestingEvent ie;
private boolean somethingHappened;

public EventNotifier (InterestingEvent event) (
/ / Save the event object for later use.
ie = event;
/ / Not to report incidents.
somethingHappened = false;
testReCall ();
)

/ **
* Callback function
*
* @ Author Ken_xu
* /
public void testReCall () (
somethingHappened = true;
doWork ();
)

/ / ...
public void doWork () (
/ / Check the possibility of setting up the predicate.
if (somethingHappened) (
/ / By calling this method interface to issue the event signals.
ie.interestingEvent ();
)
/ / ...
)

/ / ...
)

In the above example, I used to keep track of whether it should be somethingHappened predicate triggering events. In many cases, calling this method is sufficient to assure interestingEvent () signal.

The code would like to receive event notifications must implement the InterestingEvent interface, and pass their reference to the event notification process.

public class CallMe implements InterestingEvent (
private EventNotifier en;

public CallMe () (
/ / Create an event notification procedures, and pass their reference to it.
en = new EventNotifier (this);
)

int doNum = 0;

/ / For the definition of the actual event handler.
public void interestingEvent () (
/ / Oh! There must be a feel good event!
/ / Perform some operations ...
System.out.println ( "doNum =" + doNum + +);
)
/ / ...
)

This is to do all the work. I hope that this simple Java idiom will make you more confident turning to Java.
Here, I add a test function
public class TestCallMe (

/ **
* @ Param args
* @ Author Ken_xu
* /
public static void main (String [] args) (
CallMe cm = new CallMe ();
System.out.println ("====");
)

)
标签: public interface, public void, parameters, java object, c function, interface object, return values, java implementation, c code, java interfaces, object oriented model, object pointer, pointers, equivalent function, x window system, driven programming model, motif widgets, ken xu, routine method, ms windows
分类: Java
时间: 2010-03-18

相关文章

  1. java implementation queue (transfer)

    Article Category: Integrated Technology queue line actually referred to the phenomenon of life, go shopping, p ...
  2. Web server with the Java implementation

    WWW's work is based on client / server computing model, from the Web browser (client) and Web server (server) ...
  3. SSL mutual authentication java implementation

    In this paper, simulation scenario describes two-way SSL authentication java implementation By default, I thin ...
  4. Java implementation of the hard disk directory

    Java implementation of the hard disk directory
  5. I use DSL write SQL, a Java implementation

    1. REVIEW What is DSL? Domain-specific language (Domain Specific language) is usually defined as a special cla ...
  6. js the escape code java implementation transcoding

    Code from the network /** * js Codec transcoding Java implementation class . * * @author Online copy */ public ...
  7. Image verification code java implementation

    Image verification code java implementation ImageGenerator generate a random number of images containing 4 Jav ...
  8. Java implementation of the thread pool. Message Queuing function

    Java implementation of the thread pool, message queue functions
  9. Java implementation of a small software compression

    During this time the project no matter, bored to write a Java implementation of the compression software, only ...
  10. javascript java implementation of the Map (high)

    javascript java implementation of the Map, the code is abroad on a website to see ( http://stackoverflow.com/q ...
  11. Text Replacement java implementation (batch)

    Text Replacement java implementation Html in the batch will be replaced gbk character encoding UTF8: import ja ...
  12. Percentage of Java implementation

    Java implementation on the percentage of Function: The attachment is my good package on the percentage of inte ...
  13. 30 Java learning objectives (transfer)

    Self-reference: Java to learn the target time of 30 :2007-02-10 00:00:00 Source: Author: This article will tel ...
  14. Table analysis and the Java implementation of linear

    The linear table data structure, corresponding to the Collection of the List interface. In the this section, w ...
  15. Java implementation ping

    A ,<!-- [if gte mso 9]> <xml> <w:WordDocument> <w:BrowserLevel> MicrosoftInternetExplo ...
  16. iBATIS cache implementation of [transfer]

    iBATIS cache implementation of [transfer] In order to improve application performance, a more common method is ...
  17. Text clustering generated java implementation (1)

    This article is a personal study notes, continued to increase in ... This chapter to learn the Chinese word an ...
  18. Java implementation Windows system services (transfer)

    How to turn a Java program made NT Service article: From: http://blog.csdn.net/ishallwin/archive/2010/03/04/53 ...
  19. pdf word batch transfer java implementation

    java batch to achieve word turn pdf: http://topic.csdn.net/u/20070425/17/D0B419E5-1E29-410E-9245-05283F6F5918. ...