I am trying to use decorator pattern to decode and encode packet layers in my application. I've supposed my packet has three layers: Header, Level2, Level3. So far I've come up with the following code. For the sake of simplicity, I've supposed that the data of each layer is the first 10 bytes of the input packet. I've also intentionally ignored memory leak problems.
#include <iostream>
#include "Decoder.h"
int main()
{
Header* header = new Header("HeaderData");
Level2* l2 = new Level2(header, "Level2Data");
Level3* l3 = new Level3(l2, "Level3Data");
std::cout << header->Encode() << std::endl;
std::cout << l2->Encode() << std::endl;
std::cout << l3->Encode() << std::endl;
std::string packet = l3->Encode();
header->Decode(packet);
l2->Decode(header->mNextData);
l3->Decode(l2->mNextData);
std::cout << ((Header*)header)->mData << std::endl;
std::cout << ((Level2*)l2)->mData << std::endl;
std::cout << ((Level3*)l3)->mData << std::endl;
}
Decoder.h file:
#ifndef _DECODER_H_
#define _DECODER_H_
#include <string>
class DecoderBase
{
public:
virtual void Decode(std::string &packet) = 0;
virtual std::string Encode() = 0;
protected:
DecoderBase* mInnerLayer;
};
class Header : public DecoderBase
{
public:
Header(const std::string &data);
virtual ~Header();
virtual void Decode(std::string &packet);
virtual std::string Encode();
std::string mData;
std::string mNextData;
};
class Level2 : public DecoderBase
{
public:
Level2(DecoderBase* innerLayer, const std::string &data);
~Level2();
virtual void Decode(std::string &packet);
virtual std::string Encode();
std::string mData;
std::string mNextData;
};
class Level3 : public DecoderBase
{
public:
Level3(DecoderBase* innerLayer, const std::string &data);
~Level3();
virtual void Decode(std::string &packet);
virtual std::string Encode();
std::string mData;
std::string mNextData;
};
#endif
Decoder.cpp file:
#include "Decoder.h"
Header::Header(const std::string &data)
{
mData = data;
}
Header::~Header(){}
void Header::Decode(std::string &packet)
{
mData = packet.substr(0, 10);
mNextData = packet.substr(10);
}
std::string Header::Encode()
{
return mData;
}
Level2::Level2(DecoderBase *innerLayer, const std::string &data)
{
mInnerLayer = innerLayer;
mData = data;
}
void Level2::Decode(std::string &packet)
{
mData = packet.substr(0, 10);
mNextData = packet.substr(10);
}
std::string Level2::Encode()
{
return mInnerLayer->Encode() + mData;
}
Level3::Level3(DecoderBase *innerLayer, const std::string &data)
{
mInnerLayer = innerLayer;
mData = data;
}
void Level3::Decode(std::string &packet)
{
mData = packet.substr(0, 10);
mNextData = packet.substr(10);
}
std::string Level3::Encode()
{
return mInnerLayer->Encode() + mData;
}
Output:
HeaderData
HeaderDataLevel2Data
HeaderDataLevel2DataLevel3Data
HeaderData
Level2Data
Level3Data
- Am I using the decorator pattern correctly to decode and encode packets?
- Who is responsible for calling the
Decode
method of the next layer? Am I calling theDecode
method of each layer correctly? I thought maybel2->Decode()
should be called from withinheader->Decode()
.
Any advice on improving the above code would be appreciated!