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 4 2480
Manuel Kampert wrote: 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.
--
Karl Heinz Buchegger kb******@gascad .at
"Manuel Kampert" <mk******@csc.c om> wrote in message
news:76******** *************** **@posting.goog le.com... 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::QueryPo rt();
// other unique stuff
}
"jeffc" <no****@nowhere .com> wrote in message
news:3f******** @news1.prserv.n et... 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 }
should be "then the derived classes can do:"
Manuel Kampert wrote: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mike Meng |
last post by:
hi all,
I'm a newbie Python programmer with a C++ brain inside. I have a
lightweight framework in which I design a base class and expect user to
extend. In other part of the framework, I heavily use the instance of
this base class (or its children class). How can I ensure the instance
IS-A base class instance, since Python is a fully dynamic typing
language?
I searched and found several different ways to do this:
|
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 about whether we should allow polymorphism. Our system is
not embedded and does not need to be as real-time as, say, a
pacemaker. But it does involve updating displays based on radar
input. So a need for something close to real-time is desired...
|
by: E. Robert Tisdale |
last post by:
polymorph just means "many form(s)".
The definition in plain English
http://www.bartleby.com/61/66/P0426600.html
and narrower definitions in the context of computer programming
http://en.wikipedia.org/wiki/Polymorphism
http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?query=polymorphism&action=Search
|
by: Alf P. Steinbach |
last post by:
The eighth chapter (chapter 2.1) of my attempted Correct C++ tutorial is now
available, although for now only in Word format -- comments welcome!
Use the free & system-independent Open Office if you don't have Word.
Classes
<url: http://home.no.net/dubjai/win32cpptut/w32cpptut_02_01.zip>
Introduces the C++ language feature used to define new types, namely
classes. The focus in on creating safe and reusable classes. As a main
|
by: richard pickworth |
last post by:
Can anyone explain polymorphism?(very simply).
thanks
richard
| |
by: Alf P. Steinbach |
last post by:
So, I got the itch to write something more...
I apologize for not doing more on the attempted "Correct C++ Tutorial"
earlier, but there were reasons.
This is an UNFINISHED and RAW document, and at the end there is even pure
mindstorming text left in, but already I think it can be very useful.
<url: http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01__alpha.doc.pdf>.
|
by: Mike |
last post by:
Hello NG,
i am just learning various Design Patterns and now i am not sure, if
this design is correct (Builder) or if i should use an other pattern.
I have various classes (here ChildA and ChildB) derived from class Base.
Now i want to create an object, but i don't want to know which class to
instantiate.
|
by: sarathy |
last post by:
Hi all,
I need a small clarification reg. Templates and
Polymorphism. I believe templates is really a good feature, which can
be used to implement generic functions and classes. But i doubt whether
it should not be used in certain cases.
Consider the case when all the params to a template
function/class are similar. My questions is that whatever can be
acheived by a template in such a case, can be acheived by runtime
polymorphism....
|
by: chsalvia |
last post by:
I've been programming in C++ for a little over 2 years, and I still
find myself wondering when I should use polymorphism.
Some people claim that polymorphism is such an integral part of C++,
that anybody who doesn't use it might as well just program in plain
C. I totally disagree with this, because I think C++ has a lot of
great features apart from polymorphism, such as the ability to
organize code into classes, code reuse through...
|
by: Bart Friederichs |
last post by:
Hello,
I created the following inheritance:
class Parent {
public:
void foo(int i);
};
class Child : public Parent {
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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: 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: 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...
| |