Mechanism: Callback

Intent

The callback mechanism enables an application to invoke an externally supplied function (sometime called a "handler") in

A short statement that answers the following questions: What does the design pattern do? What is its rationale and intent? What particular design issue or problem does it address?

Also Known As

?

Motivation

A scenario that illustrates a design problem and how the class and object structures in the pattern solve the problem. The scenario will help you understand the more abstract description of the pattern that follows.

Applicability

What are the situations in which the design pattern can be applied? What are examples of poor designs that the pattern can address? How can you recognize these situations?

Collaborations

How the participants collaborate to carry out their responsibilities.

Consequences

Callbacks have the following benefits:

  1. They support late binding. Any user-defined function that matches the function signature of the callback can be bound to the invocation.

Callbacks have the following limitations:

  1. They lack compositionality. Only one callee can be registered for a callback at any given point in time. This prevents multiple, independent callees from registering for the same callback. The lack of a "chaining" mechanism (e.g., CLOS's call-next-method) restricts the utility of the mechanism.
  2. They can disturb time sensitive processing. The caller does not know how long the callee will execute before regaining the thread of control. For example, a time consuming callback can starve GUI event processing.

Known Uses

Callbacks are commonly used in GUI toolkits as a way of invoking application-specified code in response to a UI event. For example, Microsoft's Win32 API associates each window with an event handler responsible for responding to the event.

Related Mechanisms

Object-oriented inheritance has become a more popular mechanism for implementing callback-like behavior in OO languages. Consider, for example, GUI frameworks such as Microsoft's MFC, Sun's Java AWT, Apple's MacApp, and Unix's InterViews.


Back to the Adaptation home page. This page maintained by Peyman Oreizy (peymano at ics.uci.edu). Last updated on .