R
Rob Clark
Hello all,
I have a design problem for which my current C++ solution
feels more like a hack. :-( I'd like to change that
The real problem can be destilled to something like this:
One client wants to send data using one specific top layer
protocol. At this top layer, there are a number of different
protocols, where each one inherits the main Protocol class.
And every protocol subclass in turn contains several different
communication channels that inherits the Channel class.
So, a client knows about several different high level protocols
and each protocol has many channels.
Now, when data is to be sent, we must send that data over the
"most appropriate" communication channel. This channel can be
found by traversing the hierarchy of protocols and channels.
However, after we have found the "best" channel, we cannot send
data through this channel directly! Instead we must send data
through the specific protocol which the selected channel belongs to.
Therefore, one can let each channel be aware of its parent protocol,
and this allows for a solution to expressed in C++ code like:
void Client::SendMessage()
{
Channel* channel = GetBestChannel();
Protocol* protocol = channel->GetProtocol();
protocol->SendData(channel,"Hello world!");
}
where
void ProtocolX::SendData(const Channel* channel, const char* data)
{
// First...
DoSomeProtocolXSpecificStuff();
// Then send the data through the selected channel
channel->SendData(data);
}
The things that I do not like about this solution are that I do not
want the Channel class to be unveiled (the client shouldn't
care about the channels, it shouldn't be aware of their existance)
and, in addition to this, I do not like that each channel must be
aware of its parent protocol.
So, is there another solution for this recurring(?) problem, maybe
realized by a nifty template-technique?
Or do I simply have to accept my current naive solution?
Thanks in advance!
/Rob
I have a design problem for which my current C++ solution
feels more like a hack. :-( I'd like to change that
The real problem can be destilled to something like this:
One client wants to send data using one specific top layer
protocol. At this top layer, there are a number of different
protocols, where each one inherits the main Protocol class.
And every protocol subclass in turn contains several different
communication channels that inherits the Channel class.
So, a client knows about several different high level protocols
and each protocol has many channels.
Now, when data is to be sent, we must send that data over the
"most appropriate" communication channel. This channel can be
found by traversing the hierarchy of protocols and channels.
However, after we have found the "best" channel, we cannot send
data through this channel directly! Instead we must send data
through the specific protocol which the selected channel belongs to.
Therefore, one can let each channel be aware of its parent protocol,
and this allows for a solution to expressed in C++ code like:
void Client::SendMessage()
{
Channel* channel = GetBestChannel();
Protocol* protocol = channel->GetProtocol();
protocol->SendData(channel,"Hello world!");
}
where
void ProtocolX::SendData(const Channel* channel, const char* data)
{
// First...
DoSomeProtocolXSpecificStuff();
// Then send the data through the selected channel
channel->SendData(data);
}
The things that I do not like about this solution are that I do not
want the Channel class to be unveiled (the client shouldn't
care about the channels, it shouldn't be aware of their existance)
and, in addition to this, I do not like that each channel must be
aware of its parent protocol.
So, is there another solution for this recurring(?) problem, maybe
realized by a nifty template-technique?
Or do I simply have to accept my current naive solution?
Thanks in advance!
/Rob