Protocol Stack Design Pattern

Intent

We have already seen that Protocol Layer and Protocol Packet provide a standardized interface between different layers of a protocol. The Protocol Stack design pattern takes advantage of the layer decoupling and provides a mechanism for dynamic insertion and removal of protocol layers from a stack.

Also Known As

Motivation

Protocol stacks tend to be rigid in design and protocol layers cannot be dynamically added or removed from a protocol stack. This limits the use of protocol stacks in the even changing world of protocol standards. There are several scenarios where the layers in a protocol stack need to be changed on the fly. A few examples are:

The Protocol Stack design pattern addresses this issue and introduces a flexible architecture for dynamic addition and removal of protocol layers.

Applicability

The Protocol Stack Design pattern can be used to implement any type of layered protocol. It can be also used when different operations on an entity need to be performed in a pipeline fashion. Each stage of the pipeline could be modeled as a layer. This pattern is particularly useful in applications involving dynamic layer manipulation. A few applications are:

Structure

The Protocol Stack Design Pattern is implemented by the Protocol Stack class. This class maintains a doubly linked list of active layers.

Participants

The key actors of this design pattern:

Collaboration

The following diagram shows the relationship and collaboration between various classes needed for the Datalink layer example. 

Collaboration graph for protocol stack

Consequences

The Protocol Stack design pattern breaks down the rigid protocol layer structure and provides a very flexible solution where layers can be dynamically added and removed from the stack.

The figure below shows the flexibility of the pattern in supporting different layer organizations. The examples in the figure demonstrate:

Protocol stack configurations with debug layer, a loopback layer, and echo back layer and an encryption layer.

Implementation

The Protocol Stack is implemented as a single class. The class maintains a doubly linked list of Protocol Layers. Important methods of the class are:

Sample Code and Usage

The code for the Protocol Stack class is presented below:

Protocol Stack Header File

Protocol Stack Source File

Known Uses

The Protocol Stack design pattern can be used where ever a layered but decoupled organization is required.

Related Patterns/Principles