Polymorphism Design Question

J

Josh Mcfarlane

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
 
V

Victor Bazarov

Josh said:
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
 
J

Josh Mcfarlane

Victor said:
"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
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top