In a supervision tree, many of the processes have similar
structures, they follow similar patterns. For example,
the supervisors are very similar in structure. The only difference
between them is which child processes they supervise. Also, many
of the workers are servers in a server-client relation, finite
state machines, or event handlers such as error loggers.

Behaviours are formalizations of these common patterns.
The idea is to divide the code for a process in a generic part
(a behaviour module) and a specific part (a callback module).

The behaviour module is part of Erlang/OTP. To implement a
process such as a supervisor, the user only has to implement
the callback module which should export a pre-defined set of
functions, the callback functions.

An example to illustrate how code can be divided into a generic
and a specific part: Consider the following code (written in
plain Erlang) for a simple server, which keeps track of a number
of "channels". Other processes can allocate and free the channels
by calling the functions alloc/0 and free/1,
respectively.

The name of the server, in this example the atom
ch2, is hidden from the users of the client functions.
This means the name can be changed without affecting them.

The protcol (messages sent to and received from the server)
is hidden as well. This is good programming practice and allows
us to change the protocol without making changes to code using
the interface functions.

We can extend the functionality of server, without
having to change ch2 or any other callback module.

(In ch1.erl and ch2.erl above, the implementation
of channels/0, alloc/1 and free/2 has been
intentionally left out, as it is not relevant to the example.
For completeness, one way to write these functions are given
below. Note that this is an example only, a realistic
implementation must be able to handle situations like running out
of channels to allocate etc.)

Code written without making use of behaviours may be more
efficient, but the increased efficiency will be at the expense of
generality. The ability to manage all applications in the system
in a consistent manner is very important.

Using behaviours also makes it easier to read and understand
code written by other programmers. Ad hoc programming structures,
while possibly more efficient, are always more difficult to
understand.

Erlang/OTP comes with a number of components, each implementing
some specific functionality. Components are with Erlang/OTP
terminology called applications. Examples of Erlang/OTP
applications are Mnesia, which has everything needed for
programming database services, and Debugger which is used to
debug Erlang programs. The minimal system based on Erlang/OTP
consists of the applications Kernel and STDLIB.

The application concept applies both to program structure
(processes) and directory structure (modules).

The simplest kind of application does not have any processes,
but consists of a collection of functional modules. Such an
application is called a library application. An example
of a library application is STDLIB.

An application with processes is easiest implemented as a
supervision tree using the standard behaviours.