473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Polymorphism Design Question

I've create a base class for Packages that I am sending via TCP/IP, and
then deriving the different transport classes from this base class.

Now I can recreate the classes on the other end with the correct class
type, but each package will need to be passed back to a different
handling function that is not known to the transmitting program, and
may change.

Here's my question:

How should I implement handling the routing of the package on the
receiving program? I could use a switch statement to route them, which
would solve the problem, but defeats polymorphism and is evil.

However, if I add it to an overridden function such as Route() to the
Package class, it adds a dependancy to other files that the client
program may not have.

How would you pros go about handling this situation?

Thanks,
Josh McFarlane

Aug 17 '05 #1
2 1246
Josh Mcfarlane wrote:
I've create a base class for Packages that I am sending via TCP/IP, and
then deriving the different transport classes from this base class.

Now I can recreate the classes on the other end with the correct class
type, but each package will need to be passed back
"back"?
to a different
handling function that is not known to the transmitting program, and
may change.
Are you saying that Package is returning to your transmitting program,
to be handled?
Here's my question:

How should I implement handling the routing of the package on the
receiving program?
There should probably be some kind of 'PackageHandler' class, from which
you could derive 'PackageOneHandler' and 'PackageTwoHandler', depending on
the overall variety of packages you have...
I could use a switch statement to route them, which
would solve the problem, but defeats polymorphism and is evil.
It probably does. You still have to have a switch statement somewhere,
don't you? I mean, you receive a bunch of bytes and then what? You gotta
look at some bytes and then branch somehow based on their value... But
that's not the problem I guess. Well, the usual way is probably to have
some sort of registry for the Package type ID and the handler. Once you
have a handler instantiated, it should register itself in the registry so
next time a package arrives, that handler is called upon to handle the
package (or the package gets stuffed in its queue or something).
However, if I add it to an overridden function such as Route() to the
Package class, it adds a dependancy to other files that the client
program may not have.
That implies that Package should handle itself. I don't think it's right.
How would you pros go about handling this situation?


I don't know. Not enough detail available. A registry of queues for the
arriving packages and queue workers (handlers) should probably take care
of the most dynamic situation there is.

V
Aug 17 '05 #2
Victor Bazarov wrote:
"back"?
Are you saying that Package is returning to your transmitting program,
to be handled?
Sorry, bad usage of words, I meant after the package is recreated on
the receiver, it has to be dealt with. It is a new package from the
perspective of the receiving computer.
There should probably be some kind of 'PackageHandler' class, from which
you could derive 'PackageOneHandler' and 'PackageTwoHandler', depending on
the overall variety of packages you have...
[...]

It probably does. You still have to have a switch statement somewhere,
don't you? I mean, you receive a bunch of bytes and then what? You gotta
look at some bytes and then branch somehow based on their value... But
that's not the problem I guess. Well, the usual way is probably to have
some sort of registry for the Package type ID and the handler. Once you
have a handler instantiated, it should register itself in the registry so
next time a package arrives, that handler is called upon to handle the
package (or the package gets stuffed in its queue or something).


You know, I think this is what I was trying to head towards, but for
some reason thought that I had to incorporate the handler into the
package itself.
Creating a map of functions and PackageTypeIDs would solve this problem
and allow me to deal with this in a reliable way. Just wanted to make
sure I wasn't missing a way to use polymorphism vs switch statements.

Thanks for your advice,
Josh McFarlane

Aug 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
6
by: Jacek Dziedzic | last post by:
Hello! First of all please forgive me for not posting a compilable snippet, but rather a simplified piece of code with the unimportant details left out. Let's say I have two classes...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
3
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
1
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which...
12
by: feel | last post by:
Hi,All I am sure it's an old question. But I just find a interesting design about this: Polymorphism without virtual function in a C++ class. My solution is for some special case, trust me, very...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.