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::SendMes sage()
{
Channel* channel = GetBestChannel( );
Protocol* protocol = channel->GetProtocol( );
protocol->SendData(chann el,"Hello world!");
}
where
void ProtocolX::Send Data(const Channel* channel, const char* data)
{
// First...
DoSomeProtocolX SpecificStuff() ;
// 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 2 1460
"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::SendMes sage() { Channel* channel = GetBestChannel( ); Protocol* protocol = channel->GetProtocol( ); protocol->SendData(chann el,"Hello world!"); }
where
void ProtocolX::Send Data(const Channel* channel, const char* data) { // First... DoSomeProtocolX SpecificStuff() ; // 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::SendMes sage()
{
Protocol* protocol = ProtoFactory::G etProtocol( /* pass anything needed
to determine channel */ );
protocol->SendData(chann el, "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.
The most simple thing you can do is somethnig like this:
void Client::SendMes sage()
{
Protocol* protocol = GetProtocolForB estChannel();
protocol->SendData( "Hello World" );
}
//Somewhere in your code.
Protocol* GetProtocolForB estChannel()
{
return GetBestChannel( )->GetProtocol( );
}
Protocol* Channel::GetPro tocol()
{
//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, This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Anon Email |
last post by:
Hi people,
I'm learning about header files in C++. The following is code from
Bartosz Milewski:
// Code
const int maxStack = 16;
class IStack
|
by: Mark Broadbent |
last post by:
this might sound like an obvious question but I have found that usually
these two evolve at the same time.
One of the biggest reasons for creating the abstraction in the first place
(in my opinion) is to create a reusable framework that can be applied to
similar projects. However I have found that if an abstraction is created
first during the development phase, when the implementation is occurring,
the functionality or intended behaviour...
|
by: jortizclaver |
last post by:
Hi,
I'm about to develop a new framework for my corporative applications
and my first decision point is what kind of strings to use: std::string
or classical C char*.
Performance in my system is quite importante - it's not a realtime
system, but almost - and I concern about std::string performance in
terms of speed. No doubt to use std implementation is a lot easier but
I can't sacrifice speed.
|
by: johan2sson |
last post by:
The documentation for PyThreadState_SetAsyncExc says "To prevent naive
misuse, you must write your own C extension to call this". Anyone care
to list a few examples of such naive misuse?
Johan
|
by: int main(void) |
last post by:
Hi all,
What do you people think as the best libc implementation.
I'm a newbie, browsing through the open source codes of various libc
implementation.
I want to what is the best libc implementation and why ?
Regards,
Yugi
| |
by: John Nagle |
last post by:
How naive (in the sense that compiler people use the term)
is the current Python system? For example:
def foo() :
s = "This is a test"
return(s)
s2 = foo()
How many times does the string get copied?
|
by: pragy |
last post by:
Hey, can any one help me for writing a program of naive gauss
elimintaion technique?
It's a technique to solve system of simultaneous linear equations
using matrix.
thanks
|
by: Christopher Benson-Manica |
last post by:
Some recent posts got me thinking about how one might have dealt with
simplistic malloc() implementations which might return NULL for a 64K
request but might accept two 32K requests or four 16K requests. (I'm
assuming, perhaps incorrectly, that quality modern implementations
will coalesce free space as necessary and if possible to satisfy
requests.) I came up with the following (compilable but untested)
first cut at code to try a...
|
by: mjm2114 |
last post by:
Hi there,
I have a question on a naive implementation of a parallel MT that I've
done using the fortran version of MT19937ar.f posted in Prof.
Matsumoto's website. (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/
MT2002/emt19937ar.html)
First, I setup a KISS RNG (Marsaglia, 1999)
in the master node and seed it. I then use the first 4 outputs from
the master node to
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |