Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a design pattern that deals with callback mechanism?

People also ask

What is callback design pattern?

Wikipedia says. In computer programming, a callback, also known as a "call-after" function, is any executable code that is passed as an argument to other code; that other code is expected to call back (execute) the argument at a given time. Programmatic Example. Callback is a simple interface with single method.

What is callback mechanism?

A callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time [Source : Wiki]. In simple language, If a reference of a function is passed to another function as an argument to call it, then it will be called as a Callback function.

What is true about callback pattern?

It is a general concept, and it is not always associated with asynchronous operations. In fact, it simply indicates that a result is propagated by passing it to another function (the callback), instead of directly returning it to the caller.

How many types of callbacks are there?

There are two types of callbacks, differing in how they control data flow at runtime: blocking callbacks (also known as synchronous callbacks or just callbacks) and deferred callbacks (also known as asynchronous callbacks).


That would be the Observer Pattern - From Wikipedia

The observer pattern (a subset of the asynchronous publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.


It depends on how the callback is used.

Design patterns are all about communicating your intent.

If you intended to allow one or more callbacks to be registered and they can be called as notification "at some point in the future", you're talking Observer. Also -- the actual invocation of the callback in this case is usually "optional" or triggered based on some stimulus. (The callbacks may or may not ever be called)

If you intended to pass in "something to do", and that gets done in the method (or is used to "do something" during a later process) you're talking Strategy. Also -- the actual invocation usually happens.

Note that the exact same code could be either -- it's really about how you're thinking about the problem and how you want others to think about it.


callback is a form strategy design pattern


Several. Check out http://people.bu.edu/azs/teaching/cs108/2006fall/callback_pattern.pdf, and the following:

  • Visitor Pattern: http://en.wikipedia.org/wiki/Visitor_pattern

  • Observer Pattern: http://en.wikipedia.org/wiki/Observer_pattern

  • Strategy Pattern: http://en.wikipedia.org/wiki/Strategy_pattern

The most appropriate will depend on the situation. What programming language do you want to use and what do you want to do exactly?


External polymorphism - An object has a reference to a another object and a function to call on that object. It can be viewed as a single type, thus you can mix and match objects and functions to call for the event. Delegates are an example of this pattern. This is more of a C# style approach.

Observer pattern - You use an interface/base class that an object can implement and register this interface to an event. More of a Java style approach.

Check the answer I posted here for a C++ solution for delegates/external polymorphism: raw function pointer from a bound method


A good pattern description is the Service Callback design pattern. It's part of a catalog of SOA patterns, but the pattern as described can be employed with generic components that are not SOA services.

Another related pattern is the Return Address pattern described in the classic book "Enterprise Integration Patterns" by Hohpe and Woolf.

Josuttis also talks about callback in his book "SOA in Practice". He calls it the Request/Callback message exchange pattern.