Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement callback process in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to achieve the callback process in Java, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Implement the callback procedure XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" / > in Java

Using interfaces in Java to implement callback functions with the same function

Summary:

The Java interface provides a good way to implement callback functions. If you are used to calling methods by passing function pointers in an event-driven programming model, you will like this technique.

In the event-driven model of MS-windows or X-Window systems, developers are already familiar with calling processing methods by passing function pointers when certain events occur. In Java's object-oriented model, this approach is not supported, so it seems to rule out the use of this more comfortable mechanism, but this is not the case.

Java's interface provides a good mechanism for us to achieve the same effect as callbacks. The trick is to define a simple interface and define a method in the interface that we want to call.

For example, suppose that when an event occurs and we want it to be notified, 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 that controls the objects of all classes that implement the interface. Therefore, we do not need to care about any other external type information related to ourselves. This approach is better than the C function, because in C++-style code, you need to specify a data field to hold the object pointer, which is not required in Java.

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

Public class EventNotifier

{

Private InterestingEvent ie

Private boolean somethingHappened

Public EventNotifier (InterestingEvent event)

{

/ / Save the event object for later use.

Ie = event

/ / Nothing to report yet.

SomethingHappened = false

}

/ /...

Public void doWork ()

{

/ / Check the predicate, which is set elsewhere.

If (somethingHappened)

{

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

Ie.interestingEvent ()

}

/ /...

}

/ /...

}

In this example, we use the somethingHappened flag to track whether the event should be fired. In many cases, it is correct that the called method can fire the interestingEvent () method.

Code that wants to receive event notifications must implement the InterestingEvent interface and pass its own reference to the event notifier correctly.

Public class CallMe implements InterestingEvent

{

Private EventNotifier en

Public CallMe ()

{

/ / Create the event notifier and pass ourself to it.

En = new EventNotifier (this)

}

/ / Define the actual handler for the event.

Public void interestingEvent ()

{

/ / Wow! Something really interesting must have occurred!

/ / Do something...

}

/ /...

}

Thank you for reading this article carefully. I hope the article "how to realize the callback process in Java" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report