By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,369 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,369 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Dec 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Rob Clark" <se@ms.com> wrote in message
news:F%***********@nntpserver.swip.net...
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.
Dec 20 '05 #2

P: n/a
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,

Dec 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.