Protocol Layer Design Pattern


Provide a common framework for implementing different layers of a protocol stack.

Also Known As

  • Layer Design Pattern
  • ISO/OSI Layer  Design Pattern


A typical protocol layer interfaces with an upper layer and a lower layer of the protocol. In most designs there is a lot of dependency between different layers of the protocol. This makes the protocol stack implementation rigid and inflexible. Changes in one layer often percolate to other layers. The Protocol Layer Design Pattern addresses these limitations by decoupling the individual protocol layers.


  • The Protocol Layer Design Pattern can be used to implement different layers of a protocol stack.
  • 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.



Interface between protocol layers

Different layers of the protocol are structured as shown in the figure on the left hand side. Communication between layers takes place using standard interfaces defined by the Protocol Layer base class. All implementations of the protocol layer inherit from this class. The inheriting layer classes should implement the standard interfaces. The standard interfaces are:

  • Transmit is invoked by the upper layer to transfer a packet to the lower layer.
  • Handle Receive is invoked by the lower layer to transfer a packet to the upper layer.

The example on the left hand side shows the structure of a three layer protocol stack. The sequence of actions in the Transmit direction are:

  1. The application invokes the Network layer's Transmit method.
  2. The Network layer performs its actions and invokes the Transmit method for the lower layer.
  3. This invokes the Datalink layers transmit method. The Datalink layer performs the layer specific actions and invokes the lower layer's Transmit method.
  4. The Physical layer's Transmit method is invoked. This layer programs the appropriate hardware device and transmits the message.

The receive processing is similar to the transmit processing described above. Here the Handle Receive method is used to transfer the packet between layers.


The key actors of this design pattern:

  • Protocol Layer: This is the base class for all protocol layers. The individual layers interface with each other via pointers to this class. The actual type of the upper layer and lower layer classes is not known to the implementers of a certain layer.
  • Protocol Packet: This class manages addition and removal of headers and trailers for various protocol layers.


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

Protocol Layer


Protocol Layer class serves as the foundation for the Protocol Layer design pattern. This class defines the common interface for interactions between different layers of a protocol. This has several advantages:

  • The implementation of one layer is completely decoupled from the other layers. The implementers of one layer do not even need to include the header files defining the other layers.
  • Layers can be added and removed without needing any changes to the code for individual layers. (e.g. an IPSec layer can be added between the IP and Physical layers, without making any changes to the IP or the Physical Layer code).
  • A single layer could interface with multiple upper or lower layer protocols using the same interface. (e.g. an IP layer implementation could interface with an ATM or Ethernet physical layer. No changes would be needed to the code for the IP layer.


The following scenarios are supported by the Datalink Layer example of the Protocol Layer design pattern:

A packet is received from the upper layer

  1. The upper layer uses its "Lower Layer" pointer to invoke the Transmit method for the lower layer. The "Protocol Packet" to be transmitted is passed to the Transmit method.
  2. This invokes the Datalink Layer's Transmit method.
  3. The Datalink Layer passes the "Protocol Packet" to the "Transmit Protocol Handler" object.
  4. The "Transmit Protocol Handler" processes the "Protocol Packet" and adds the datalink layer header to the packet.
  5. The "Transmit Protocol Handler" uses its parent layer to obtain a pointer to the lower layer.
  6. The "Protocol Packet" is passed to the lower layer by invoking the Transmit method.

A packet is received from the lower layer

  1. The lower layer uses its "Upper Layer" pointer to invoke the "Handle Receive" method for the upper layer. The received "Protocol Packet" is passed to the "Handle Receive" method.
  2. This invokes the Datalink Layer's "Handle Receive" method.
  3. The Datalink Layer passes the "Protocol Packet" to the "Receive Protocol Handler" object.
  4. The "Receive Protocol Handler" object uses the parent layer to obtain a pointer to the upper layer.
  5. The "Protocol Packet" is passed to the higher layer by invoking the "Handle Receive" method.

Sample Code and Usage

The code for the Protocol Layer class and the Datalink Layer example is presented below:

Protocol Layer


#include <stdio.h>
class Protocol_Packet;

class Protocol_Layer
    Protocol_Layer *m_p_Lower_Layer;

    Protocol_Layer *m_p_Upper_Layer;

        m_p_Lower_Layer = NULL;
        m_p_Upper_Layer = NULL;

    virtual void Transmit(Protocol_Packet *p_Packet) = 0;

    virtual void Handle_Receive(Protocol_Packet *p_Packet) = 0;

    void Set_Upper_Layer(Protocol_Layer *p_Layer)
    {   m_p_Upper_Layer = p_Layer; }

    void Set_Lower_Layer(Protocol_Layer *p_Layer)
    {   m_p_Lower_Layer = p_Layer; }

    Protocol_Layer *Get_Upper_Layer() const
    {   return m_p_Upper_Layer; }

    Protocol_Layer *Get_Lower_Layer() const
    {   return m_p_Lower_Layer; }


Datalink Layer Header File


#include "Protocol_Layer.h"
#include "Transmit_Protocol_Handler.h"
#include "Receive_Protocol_Handler.h"

class Protocol_Packet;

class Datalink_Layer : public Protocol_Layer
    Transmit_Protocol_Handler m_transmit_Protocol_Handler;

    Receive_Protocol_Handler m_receive_Protocol_Handler;



   // Process and transmit the packet passed by higher layer.
   void Transmit(Protocol_Packet *p_Packet);

   void Handle_Receive(Protocol_Packet *p_Packet);



Datalink Layer Source File

#include "Datalink_Layer.h"
#include "Protocol_Packet.h"

Datalink_Layer::Datalink_Layer() : 
{ }

void Datalink_Layer::Transmit(Protocol_Packet *p_Packet)
    m_transmit_Protocol_Handler.Handle_Transmit_Request((Datagram *)p_Packet);

void Datalink_Layer::Handle_Receive(Protocol_Packet *p_Packet)
    m_receive_Protocol_Handler.Handle_Received_Packet((Datagram *)p_Packet);

Known Uses

  • Implementation of ISO-OSI protocol layers.
  • Modeling and implementing stages of a pipeline.

Related Patterns/Principles