What is a call back interface in Java?

后端 未结 1 1390
情深已故
情深已故 2021-02-04 08:38

This code snippet for the interface SetObserver is taken from Effective Java (Avoid Excessive Synchronization Item 67)

public interface SetObser         


        
1条回答
  •  滥情空心
    2021-02-04 09:14

    A general requirement for an interface to be a "callback interface" is that the interface provides a way for the callee to invoke the code inside the caller. The main idea is that the caller has a piece of code that needs to be executed when something happens in the code of another component. Callback interfaces provide a way to pass this code to the component being called: the caller implements an interface, and the callee invokes one of its methods.

    The callback mechanism may be implemented differently in different languages: C# has delegates and events in addition to callback interfaces, C has functions that can be passed by pointer, Objective C has delegate protocols, and so on. But the main idea is always the same: the caller passes a piece of code to be called upon occurrence of a certain event.

    0 讨论(0)
提交回复
热议问题