Hook (computer science)

from Wikipedia, the free encyclopedia

Hook ( English for hook , also called plug- in method) describes an interface in programming with which foreign program code can be integrated into an existing application in order to expand it, to change its sequence or to intercept certain events. This can be done either in the source code , which is modified accordingly, via configuration files that change the sequence of a finished program , or via calling functions to which the program code to be executed is given in some form. Typically, the default behavior of push-in methods is to do nothing.

Hooks can also be provided by the operating system to intercept messages. This z. B. implement system-wide hotkeys. For example, keyboard messages can be received via a hook regardless of the program that is currently in focus.

implementation

There are several ways to implement hooks.

If the source text of the program to be extended is available, extensions can always be made. If the code is not exactly known, however, this is time-consuming because this extension may require extensive changes, which can also have undesirable side effects and also have to be updated with each new version.

This is why some programs define certain places that are explicitly intended to be expanded by others. This can be done by calling a function that was implemented empty in the original program, i.e. it does nothing. This can then be safely diverted to an externally implemented function. The originally empty function is then the hook.

In object-oriented programming , this is done through inheritance or delegation :

  • In the case of inheritance, the hooks are empty methods that can be implemented in subclasses . An example is the stencil method design pattern ; here several methods are called one after the other as hooks in order to be able to specialize algorithms with a fixed flow frame.
  • In the case of delegation, a hook is passed to the caller in the form of an object as an attribute . This object must implement a certain interface. An example is the strategy design pattern. If the hook is expected to be used by several different service providers, the caller can implement the hook in the form of a list instead of a simple attribute, so that several implementations of the hook are called in sequence can. This is done, for example, in the case of a listener to intercept user interactions. If the list is only processed until the first implemented hook returns a certain result, you have a chain of responsibility .

Operating system hooks under Windows are implemented in a separate DLL , which is called by the operating system without a program context. A program that implements a hook (e.g. for notification of an event) must find out its results from the code contained in the DLL via other mechanisms (e.g. by means of interprocess communication ).

configuration

The question remains how the calling program gets knowledge of the program parts that want to use the hooks provided.

In the simplest case, the program code is changed directly in order to gain access to the hooks.

Or program code is integrated via configurations. This can be in the procedural programming via function pointers done by using an invoke subroutine is entered with their starting address. In object-oriented programming, on the other hand, a class is loaded dynamically using its name; this class then implements the code called by the hook as a subclass or as a delegation object. If dependency injection is used, hooks can also be easily configured here.

A third possibility is to set an object or a function which is to be called by the hook via an access function . This is done if the hook is provided by a program library that is used by the same part of the program that will also use the hooks. This is used, for example, with the listener for user interactions, which was already mentioned above.

See also

Individual evidence

  1. Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides: Design pattern . Addison-Wesley, Munich 2011, ISBN 978-3-8273-3043-7 , pp. 370 ( What is an insertion method ? "Insertion methods that offer a default behavior that can be expanded by subclasses if necessary. An insertion method often does nothing by default." - English: Design Patterns . Translated by Dirk Riehle).