C
coeng
I've run into a little problem with an OO design.
Suppose I have a class called "Message".
Each time I receive a communication from an external source, I
instantiate an object of class "Message".
A "Message" object consists of five 16-bit words and is a raw copy of
the payload of the communication just received.
A "Message" object contains various fields of data, some short in
length, some long, and some of which can span across word boundaries.
Some of are interest to my application, others are not. Some fields
are common to all messages, others are common to a subset.
The first "n" bits of the 1st 16-bit word are used to identify the
"message type".
The "message type" is then in turn, used to determine how to decode
the remaining fields in the remaining words of the message.
For example, if the message type is "message A" then "fieldX" appears
in the fourth word. If the message type is "message B" then "fieldX"
appears in the fifth word. Assume there exist "messages C thru Z".
I basically want to do something like this (in pseudocode):
instantiate a Message object upon receiving message from a source;
decode fieldX with something like...(fieldX = thisMsg->getfieldX();
What I don't want is something like this:
instantiate a Message object upon receiving message from a source;
if (thisMsg == message A) fieldX = extract bits from 4th word
if (thisMsg == message B) fieldX = extract bits from 5th word
if (thisMsg == message C) fieldX = extract bits from 1st word
.....
.....
if (thisMsg == message Z) fieldX = extract bits from 2nd word
I was doing some reading on the bridge pattern and how I could
possibly apply it here, but I don't feel like it would apply in this
situation. In order to implement the bridge pattern, the
implementation (i.e. the correct bit extraction for fieldX) needs to
be known for the abstraction (i.e. the particular message type) a
priori. The first "n" bits of a message determine how fieldX is
extracted. This information is not known until a Message object is
created and its first "n" bits are decoded.
Any help would be appreciated. Thanks.
Suppose I have a class called "Message".
Each time I receive a communication from an external source, I
instantiate an object of class "Message".
A "Message" object consists of five 16-bit words and is a raw copy of
the payload of the communication just received.
A "Message" object contains various fields of data, some short in
length, some long, and some of which can span across word boundaries.
Some of are interest to my application, others are not. Some fields
are common to all messages, others are common to a subset.
The first "n" bits of the 1st 16-bit word are used to identify the
"message type".
The "message type" is then in turn, used to determine how to decode
the remaining fields in the remaining words of the message.
For example, if the message type is "message A" then "fieldX" appears
in the fourth word. If the message type is "message B" then "fieldX"
appears in the fifth word. Assume there exist "messages C thru Z".
I basically want to do something like this (in pseudocode):
instantiate a Message object upon receiving message from a source;
decode fieldX with something like...(fieldX = thisMsg->getfieldX();
What I don't want is something like this:
instantiate a Message object upon receiving message from a source;
if (thisMsg == message A) fieldX = extract bits from 4th word
if (thisMsg == message B) fieldX = extract bits from 5th word
if (thisMsg == message C) fieldX = extract bits from 1st word
.....
.....
if (thisMsg == message Z) fieldX = extract bits from 2nd word
I was doing some reading on the bridge pattern and how I could
possibly apply it here, but I don't feel like it would apply in this
situation. In order to implement the bridge pattern, the
implementation (i.e. the correct bit extraction for fieldX) needs to
be known for the abstraction (i.e. the particular message type) a
priori. The first "n" bits of a message determine how fieldX is
extracted. This information is not known until a Message object is
created and its first "n" bits are decoded.
Any help would be appreciated. Thanks.