Is there a design pattern that deals with callback mechanism?
10 Answers
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.

- 50,714
- 13
- 121
- 117
-
6The Observer pattern is similar to the publish-subscribe pattern, not callback. For once, the Observer modules are not "called back" after calling the Observable module. The Observable calls the Observers to notify them of state changes. – Paulo Merson Jan 22 '16 at 21:01
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.

- 29,742
- 9
- 47
- 65
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?
-
2_Visitor_ implements double-dispatch, which has nothing to do with callbacks. _Observer_ implements publish-subscribe, which is similar to callbacks, but different. _Strategy_ implements first-class functions, which have nothing to do with callbacks. – jaco0646 Oct 08 '18 at 19:59
-
1I also replied to you in the previous answer; there is nothing about first-class functions that makes them unusable as callbacks. In fact, the strategy pattern is probably the simplest way to explain callback functions to someone, since it doesn't require introducing the concept of async execution. – BigJMoney May 07 '22 at 03:32
callback is a form strategy design pattern

- 114,442
- 31
- 189
- 228
-
3I agree that strategy is a better analogy then Observer. To me a callback would be a function pointer or a closure. Since those constructs aren't available in all languages, a strategy is the closet approximation. Depending on your perspective you have the (mis)fortune of having to create the various interfaces required by your design pattern of choice. – Patrick Jun 03 '09 at 20:22
-
2_Strategy_ implements first-class functions, which have nothing to do with callbacks. – jaco0646 Oct 08 '18 at 20:00
-
First class functions can indeed be callbacks see https://developer.mozilla.org/en-US/docs/Glossary/First-class_Function, and https://developer.mozilla.org/en-US/docs/Glossary/Callback_function – BigJMoney May 07 '22 at 03:29
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.

- 13,270
- 8
- 79
- 72
Your question is very general, and the most general answer I can think of is to use polymorphism when you have a problem requiring a callback.
Polymorphism allows you to specify a software contract in form of an interface (or an abstract class) about how your callback is to be used. Then clients are free to choose any implementation of the interface they see fit for their purpose.
Whether it is advisable to use the state, strategy, observer pattern or something completely different really depends on the circumstances.

- 6,388
- 4
- 39
- 64
Page 235 of Design Patterns
"Commands (Command Pattern) are an object-oriented replacement for callbacks."

- 17,736
- 16
- 35
- 75
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/31259932) – beeselmane Mar 14 '22 at 19:02
The Observer Pattern comes to mind.
One object (suscriber) can suscribe to another object (publisher). When the publisher changes or is updated, he can notify all his suscribers.
Depending on what language you are using, you can specify a function that should be called on notifies.
http://en.wikipedia.org/wiki/Observer_pattern
It is well described in Design Patterns: Elements of Reusable Object-Oriented Software [Gang of Four]

- 7,361
- 6
- 40
- 50
I agree with the other posters about the Observer pattern as well. It's specifically designed for this purpose.

- 53
- 1
- 5