Correct use of polymorphism

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Manuel Kampert

    Correct use of polymorphism

    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
  • Karl Heinz Buchegger

    #2
    Re: Correct use of polymorphism



    Manuel Kampert wrote:[color=blue]
    >
    > 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)[/color]

    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.
    [color=blue]
    >
    > 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)[/color]

    Thats worse.
    [color=blue]
    >
    > Or maybe I am not thinking object oriented at all?[/color]

    I think you are fine.

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • jeffc

      #3
      Re: Correct use of polymorphism


      "Manuel Kampert" <mkampert@csc.c om> wrote in message
      news:7688a07.03 11110514.6fc27a 97@posting.goog le.com...[color=blue]
      >
      > 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)[/color]

      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::QueryPo rt();
      // other unique stuff
      }


      Comment

      • jeffc

        #4
        Re: Correct use of polymorphism


        "jeffc" <nobody@nowhere .com> wrote in message
        news:3fb14087_2 @news1.prserv.n et...[color=blue]
        >
        > Have you considered maknig the function pure virtual AND defining that[/color]
        part[color=blue]
        > 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::QueryPo rt();
        > // other unique stuff
        > }[/color]

        should be "then the derived classes can do:"


        Comment

        • Bruno Desthuilliers

          #5
          Re: Correct use of polymorphism

          Manuel Kampert wrote:[color=blue]
          > Hi,
          >[/color]
          (snip)
          [color=blue]
          > 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)[/color]

          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.
          [color=blue]
          > 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)[/color]

          Yuck !
          [color=blue]
          > Or maybe I am not thinking object oriented at all?[/color]

          Seems ok !-)

          My 2 cents
          Bruno

          Comment

          Working...