Skip to content

Instantly share code, notes, and snippets.

@achref-abidi
Last active April 9, 2023 23:22
Show Gist options
  • Save achref-abidi/7e2287e80f468b6b65e70b6f1657a282 to your computer and use it in GitHub Desktop.
Save achref-abidi/7e2287e80f468b6b65e70b6f1657a282 to your computer and use it in GitHub Desktop.
An assignment for using decorator pattern

Assignment: Implement a packet encoder/decoder using Decorator pattern

You have been tasked with implementing a packet encoder and decoder system for a network communication protocol. The protocol involves adding several layers of headers, trailers, and other metadata to packets, which need to be encoded and decoded in a specific order.

Your task is to use the Decorator pattern to implement a system that allows for flexible addition and removal of these layers of packet metadata, while ensuring that the packets are correctly encoded and decoded according to the protocol.

Requirements:

  • The system should have a base Packet class that represents the actual packet data.
  • The system should have several decorator classes that add different layers of metadata to the packet, such as headers, trailers, and checksums.
  • The decorators should be stackable and order-specific, meaning that the order in which they are added or removed affects the final encoding and decoding of the packet.
  • The system should have an encoder class that takes in a Packet object and applies all the necessary decorators to encode the packet according to the protocol.
  • The system should have a decoder class that takes in an encoded packet and applies the necessary decorators in reverse order to decode the packet data.

Implementation:

You are required to implement the following classes:

  • Packet class: Represents the actual packet data. It should have methods to set and get the packet data.
  • PacketEncoder class: Applies the necessary decorators to encode the packet according to the protocol. It should have a method to encode the packet data.
  • PacketDecoder class: Applies the necessary decorators in reverse order to decode the packet data. It should have a method to decode the encoded packet data.
  • Decorator class (abstract): Base class for all packet metadata decorators. It should have a reference to the Packet object and define methods to add or remove metadata layers.
  • HeaderDecorator class: Adds a header to the packet. It should have a method to set the header data.
  • TrailerDecorator class: Adds a trailer to the packet. It should have a method to set the trailer data.
  • ChecksumDecorator class: Adds a checksum to the packet. It should have a method to calculate the checksum and verify it during decoding.

Testing:

You should write unit tests to verify that the packet encoder and decoder work correctly with different combinations of decorators and packet data. The tests should cover the following scenarios:

  • Encoding and decoding a packet with only one decorator.
  • Encoding and decoding a packet with multiple decorators in different orders.
  • Encoding and decoding a packet with invalid checksum and verifying that decoding fails.

Submission:

Submit your C++ code along with the unit tests and a brief report describing your implementation and testing process.

Bonus:

you can use other design patterns in addition to the decorator pattern to solve the same problem or to enhance your solution. For example, you could use the factory method pattern to create the concrete components that the decorator pattern will decorate. You could also use the strategy pattern to define different algorithms for encoding and decoding, and the decorator pattern can be used to add additional layers of encoding or decoding. Additionally, you could use the observer pattern to notify interested parties when a new layer of encoding or decoding is added to the packet. There are many possibilities, and it depends on the specific requirements and constraints of your project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment