Hook is a programming mechanism to provide future functionality expansions. An hook is often contained in a library, whereas the expansion is a function written by the developer.
The boundary between hook and callback is very fuzzy and from the coding point of view they are essentially the same. But we express a different use and context with each term.
A callback is usually called after an operation has been completed. It's for example possible register a callback to be called back after an ajax call has been completed.
A hook is a more generic term to indicate a code expansion and it's usually more common in low level programming.
There are essentially two types of hooks:
There is essentially one type of callback:
Let's see some examples.
Here's a callback with delegation in AWT: JButton button = new JButton("Execute"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("I'm an event expansion"); } });
JButton is the delegator, whereas the anonymous class is the delegate, that expands the functionality for a click event.
Here's is an example of hook with override:
public abstract class Builder { protected void preProcess() {} public void build() { preProcess(); // Operations to build } }
preProcess() is the hook, which does nothing, until when a subclass expands the method.
Here's an other example of hook with override, from the AWT source code:
public abstract class MouseAdapter implements MouseListener { public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }
MouseAdapter is an implementation of the adapter pattern and contains a series of hooks that allows the developer to implement only the method that he really needs, but not all the methods of the interface MouseListener.
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.