What is the difference between hook and callback?

coanor picture coanor · Jun 18, 2012 · Viewed 13.6k times · Source

By reading some text, especially the iOS document about delegate, all the protocol method are called hook that the custom delegate object need to implement. But some other books, name these hook as callback, what is the difference between them? Are they just different name but the same mechanism? In addition to Obj-C, some other programming languages, such as C, also got the hook, same situation with Obj-C?

Answer

Attila picture Attila · Jun 18, 2012

The terminology here is a bit fuzzy. In general the two attempt to achieve similar results.

In general, a callback is a function (or delegate) that you register with the API to be called at the appropriate time in the flow of processing (e.g to notify you that the processing is at a certain stage)

A hook traditionally means something a bit more general that serves the purpose of modifying calls to the API (e.g. modify the passed parameters, monitor the called functions). In this meaning it is usually much lower level than what can be achieved by higher-level languages like Java.

In the context of iOS, the word hook means the exact same thing as callback above