Is my naive C++ implementation the best, or...

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
 
J

Jim Langston

Rob Clark said:
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.

If the channel shouldn't be unveiled to the client, why don't you have the
channel defined in the protocol? Have the protocol do the
channel->GetProtocol(); in it's constructor. I.E. You want a class factory
that actually returns the protocol.

The call would probably look something like this (I've not used factories,
just understand them a bit in theory and what I've read here).

void Client::SendMessage()
{
Protocol* protocol = ProtoFactory::GetProtocol( /* pass anything needed
to determine channel */ );
protocol->SendData(channel, "Hello world!");
}

So you have your ProtoFactory (as I understand it a class) that would do the
channel selection for you, create the protocol using new and return the
pointer to it.

If you search message topics here for "factory" you should find some code
(although it may not be the best) or google for it.
 
A

AbdulMunaf

The most simple thing you can do is somethnig like this:

void Client::SendMessage()
{
Protocol* protocol = GetProtocolForBestChannel();
protocol->SendData( "Hello World" );
}

//Somewhere in your code.
Protocol* GetProtocolForBestChannel()
{
return GetBestChannel()->GetProtocol();
}

Protocol* Channel::GetProtocol()
{
//Find the protocol and instantiate.
// You can use factory or abstract factory method .
//Pass this pointer in constructor of Protocol.
}

class Protocol
{
Channel* channel_;

public:
Protocol( Channel* channel ) : channel_( channel )
{
}

//other methods...
};

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top