Wrapper functions are a means of
delegation and can be used for a number of purposes.
Programming convenience Wrapper functions simplify writing computer programs. For example, the MouseAdapter and similar classes in the
Java AWT library demonstrate this. They are useful in the development of applications that use third-party library functions. A wrapper can be written for each of the third party functions and used in the native application. In case the third party functions change or are updated, only the wrappers in the native application need to be modified as opposed to changing all instances of third party functions in the native application.
Adapting class/object interfaces Wrapper functions can be used to
adapt an existing class or object to have a different interface. This is especially useful when using existing library code.
Code testing Wrapper functions can be used to write
error checking routines for pre-existing system functions without increasing the length of a code by a large amount by repeating the same error check for each call to the function. All calls to the original function can be replaced with calls to the wrapper, allowing the programmer to forget about error checking once the wrapper is written. A test driver is a kind of wrapper function that exercises a code module, typically calling it repeatedly, with different settings or parameters, in order to rigorously pursue each possible path. It is not deliverable code, but it is not throwaway code either, being typically retained for use in
regression testing. An interface adaptor is a kind of wrapper function that simplifies, tailors, or amplifies the interface to a code module, with the intent of making it more intelligible or relevant to the user. It may rename parameters, combine parameters, set defaults for parameters, and the like.
Multiple inheritance In a
programming language that does not support
multiple inheritance of base classes, wrapper functions can be used to simulate it. Below is an example of part of a
Java class that "inherits" from java.util.LinkedList and java.util.HashSet. See
method for further implementation details. import java.util.HashSet; import java.util.LinkedList; public class Test implements LinkedList, HashSet { @Override // contains data members and data methods // covariant return } ==Library functions and system calls==