Correct use of polymorphism

M

Manuel Kampert

Hi,

while playing around as an hobby programmer for the last decade
I have now decided to dive deeper into the object oriented design
and programming paradigm.

To learn more about the OO story I have decided to write a small
network management system.

I have created the following (simplified) object model:




------------ ------------
| | /\| |
| Device |--------------| Ports |
| |1 n\/| |
------------ ------------
/|\
|
-------------|-------------
| | |
------------ ------------ ------------
| | | | | |
| HP | | Cisco | | Telesyn |
| | | | | |
------------ ------------ ------------



the classes HP, Cisco and Telesyn represent an switch or an hub on an
network.
Every such device has an amout of ports that it makes available to the
network to connect PCs, printers, workstation and the like to it.

Each switch or hub on the network provides a way to see which device
(address)
is connected to its port. The way how this is managed differs from
manufacturer
to manufacturer (i.e. SNMP query ifTable, SNMP query some entersprise
specific information base, use telnet,...).

So what I have at the moment is an pure virtual function in the
Device object named "QueryPorts() = 0;" This function is defined in
the classes HP, Cisco and Telesyn and querys for an address connected
to a port.

The algorithm that I use to get the device address from a hub or a
switch, update the database, check if the port already exists on the
database, ... is - except on some detail on how to ask the port for
the mac address - identical.

My question here is if it is better to try to write the algorithm as
generic
as possible in the base class and then add only what differs in the
derived
class? (of course not writing QueryPorts() as pure virtual, but having
some "pure virtual helper function)

Or is it better to copy and paste the alorithm to the derived classes
and leave the pure virtual function as it is (having to duplicate most
of the algorithm in all the derived classes)

Or maybe I am not thinking object oriented at all?


Many thanks in advance,
Manuel
 
K

Karl Heinz Buchegger

Manuel said:
So what I have at the moment is an pure virtual function in the
Device object named "QueryPorts() = 0;" This function is defined in
the classes HP, Cisco and Telesyn and querys for an address connected
to a port.

The algorithm that I use to get the device address from a hub or a
switch, update the database, check if the port already exists on the
database, ... is - except on some detail on how to ask the port for
the mac address - identical.

My question here is if it is better to try to write the algorithm as
generic
as possible in the base class and then add only what differs in the
derived
class? (of course not writing QueryPorts() as pure virtual, but having
some "pure virtual helper function)

Generic is always better.

But you could eg. do:

Move all hardware dependent functionality into functions of their own
(make them virtual). Now write the generic QueryPorts function in your
base class. Make sure that this function uses only those (hardware
dependent) virtual functions you introduced earlier.

This way adding a new device means:
Implement the hardware dependent stuff in the new class and the generic
algorithm will work for this device too.

Another possibility would be to change your object model:
Divide your model into:

A Hub class
A Router class
A ... class

All those classes contain the generic algorithms. In order to access
the real hardware device, you introduce

class HubDevice
class RouterDevice
...

and derive from them eg.

class HPHub : public HubDevice
class CiscoHub : public HubDevice

class TelesynRouter : public RouterDvice

Now you give every Hub class an instance of the device class it should use
to access the real device. The HpHub, CiscoRouter, TelesynHub etc. classes
work like device drivers this way.
Or is it better to copy and paste the alorithm to the derived classes
and leave the pure virtual function as it is (having to duplicate most
of the algorithm in all the derived classes)

Thats worse.
Or maybe I am not thinking object oriented at all?

I think you are fine.
 
J

jeffc

Manuel Kampert said:
My question here is if it is better to try to write the algorithm as
generic
as possible in the base class and then add only what differs in the
derived
class? (of course not writing QueryPorts() as pure virtual, but having
some "pure virtual helper function)

Or is it better to copy and paste the alorithm to the derived classes
and leave the pure virtual function as it is (having to duplicate most
of the algorithm in all the derived classes)

Have you considered maknig the function pure virtual AND defining that part
that is common in the base? That's what I'd do right off the top of my
head. Make it pure virtual so subclasses must redefine it, but write code
for it too. Then the base classes can do:

HP::QueryPorts()
{
Device::QueryPort();
// other unique stuff
}
 
J

jeffc

jeffc said:
Have you considered maknig the function pure virtual AND defining that part
that is common in the base? That's what I'd do right off the top of my
head. Make it pure virtual so subclasses must redefine it, but write code
for it too. Then the base classes can do:

HP::QueryPorts()
{
Device::QueryPort();
// other unique stuff
}

should be "then the derived classes can do:"
 
B

Bruno Desthuilliers

Manuel said:
Hi,
(snip)

So what I have at the moment is an pure virtual function in the
Device object named "QueryPorts() = 0;" This function is defined in
the classes HP, Cisco and Telesyn and querys for an address connected
to a port.

The algorithm that I use to get the device address from a hub or a
switch, update the database, check if the port already exists on the
database, ... is - except on some detail on how to ask the port for
the mac address - identical.

My question here is if it is better to try to write the algorithm as
generic
as possible in the base class and then add only what differs in the
derived
class? (of course not writing QueryPorts() as pure virtual, but having
some "pure virtual helper function)

You want to have a look at the 'template method' pattern.

in short :
The 'generic' part of the algorithm is coded in a method in the base
class. This method calls virtual helpers methods for the 'specific'
parts of the algorithm. Then you just have to write the implementation
for theses specific virtual helpers methods in the derived classes, and
the 'generic' method of the base class will take care of the rest.

You can of course keep the 'generic' method virtual, and override it in
a derived class is the algorithm for this class is really different from
the generic one.
Or is it better to copy and paste the alorithm to the derived classes
and leave the pure virtual function as it is (having to duplicate most
of the algorithm in all the derived classes)

Yuck !
Or maybe I am not thinking object oriented at all?

Seems ok !-)

My 2 cents
Bruno
 

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
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top