473,888 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about class design

I'm still learning how to use Object Oriented concepts. I'm have a
basic question dealing with design. I have two classes that deal with
I/O pertaining to network and usb that inherit from an abstract parent
with four basic functions: Open, Read, Write, and Close.

// Note alot of the stuff has been left out
// just to give a basic picture
class IO
{
public:
virtual int Open(..) =0;
virtual int Read(..) =0;
virtual int Write(..) =0;
virtual int Close(..)=0;
};

class network : public IO{
....
};

class usb: public IO {
...
};

Now I want to create a class that uses the network or usb to
read/write. The different ways of doing these and I'm not sure which is
appropriate. Here my inital thoughts

1)
class MyProtocol
{
IO* m_io;

public:
MyProtocol(IO *);
};

Here I could give my class MyProtocol either Network or Usb and it
wouldn't care. The other choice
2)
class MyNetworkProtoc ol: public Network
{};

class MyUsbProtocol: public Usb
{ };

Here I will create two classes for each one but each class would have
the same logic.

These are the two choices that I thought of but I'm not sure which one
is appropriate. I'm still unsure about the design, I'm open to any
advice. Thanks

Danny

Aug 19 '05 #1
3 1822
<fe***********@ gmail.com> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com...
I'm still learning how to use Object Oriented concepts. I'm have a
basic question dealing with design. I have two classes that deal with
I/O pertaining to network and usb that inherit from an abstract parent
with four basic functions: Open, Read, Write, and Close.

// Note alot of the stuff has been left out
// just to give a basic picture
class IO
{
public:
virtual int Open(..) =0;
virtual int Read(..) =0;
virtual int Write(..) =0;
virtual int Close(..)=0;
virtual ~IO() {} // or is that one of the things left out?
};

class network : public IO{
....
};

class usb: public IO {
...
};

Now I want to create a class that uses the network or usb to
read/write.
Why? An IO* points to exactly such a class. It seems to me you're done.
The different ways of doing these and I'm not sure which is
appropriate. Here my inital thoughts

1)
class MyProtocol
{
IO* m_io;

public:
MyProtocol(IO *);
};
Pointless. This just wraps an IO* in a class. An IO is already a class.

Here I could give my class MyProtocol either Network or Usb and it
wouldn't care. The other choice
2)
class MyNetworkProtoc ol: public Network
{};

class MyUsbProtocol: public Usb
{ };


Polymorphism is for when you want to treat a family of classes as being the
same type when they actually aren't. For instance, you could have an array
of IO pointers and call Open on all the objects without knowing or caring
which type they actually are. The only thing you really could use is a smart
pointer:

#include "boost/shared_ptr.hpp"
typedef boost::shared_p tr<IO> IOPtr;

That will save you a lot of memory leak problems.

[snip]

--
Cy
http://home.rochester.rr.com/cyhome/
Aug 19 '05 #2
* fe***********@g mail.com:
I'm still learning how to use Object Oriented concepts. I'm have a
basic question dealing with design. I have two classes that deal with
I/O pertaining to network and usb that inherit from an abstract parent
with four basic functions: Open, Read, Write, and Close.

// Note alot of the stuff has been left out
// just to give a basic picture
class IO
{
public:
virtual int Open(..) =0;
virtual int Read(..) =0;
virtual int Write(..) =0;
virtual int Close(..)=0;
};

class network : public IO{
....
};

class usb: public IO {
...
};

Now I want to create a class that uses the network or usb to
read/write. The different ways of doing these and I'm not sure which is
appropriate. Here my inital thoughts

1)
class MyProtocol
{
IO* m_io;

public:
MyProtocol(IO *);
};

Here I could give my class MyProtocol either Network or Usb and it
wouldn't care. The other choice
2)
class MyNetworkProtoc ol: public Network
{};

class MyUsbProtocol: public Usb
{ };

Here I will create two classes for each one but each class would have
the same logic.
Assuming that the same protocol _may_ be used with both 'network' and 'usb'
choice 2 would mean redundant code, which is nearly always Evil (e.g., think
of maintenance, keeping two corresponding implementations in synch).

Also, the inheritance should probably not be public, because a protocol
isn't an 'io': you wouldn't want client code to circumvent the protocol,
calling 'read' and 'write' directly, would you?

However, you could do something like choice 2 as

3)
template< class IoProvider >
class Protocol: private IoProvider
{
...
};

The difference between choices 1 and 3 is that 1 provides _run-time_
selection of the IoProvider, while 3 provides _compile-time_ selection.

Choice 1 is far more flexible. While choice 3 can help you avoid things
like using an inappropriate Protocol/IoProvider combination, catching such
errors at compile time (e.g. a partial specialization with no
implementation) I don't think that's very much of an advantage in practice.
Also, choice 3 is more difficult to implement and debug.

These are the two choices that I thought of but I'm not sure which one
is appropriate. I'm still unsure about the design, I'm open to any
advice. Thanks


It may be that also Protocol should be designed as a pluggable class. And
perhaps it might be a good idea to think about protocol stacks, i.e.
protocols plugged into protocols for a few levels. But whether you need
that depends on the application you have in mind, and since generality
nearly always has some cost, both in development time and run-time, it is a
matter of biting nails, throwing dice, soliciting advice, perusing budgets,
checking what others have done, and so on, so as not to overdo generality.

Final word of advice: think about whether Protocol should be directly used
by client code.

Perhaps the design will be a lot cleaner if Protocol is designed as
pluggable into some class X that has a public interface adapted exclusively
to the client code's needs (implementing X might help design Protocol)?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 19 '05 #3
Thanks for your advice. Sometimes it's hard to think of the design
choices when you are your own client, just writing my own code for
myself.
Perhaps the design will be a lot cleaner if Protocol is designed as
pluggable into some class X that has a public interface adapted exclusively
to the client code's needs (implementing X might help design Protocol)?


Right on, I do have a class that contains my class Protocol. At the
time, I wrote it to make it cleaner to call my Protocol class. Well
thanks for the advice.

Aug 20 '05 #4

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

Similar topics

51
4339
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class, a method (event) needs to be able to invoke methods of the second class. With static classes its possible but this is not desirable. There's obviouly some visibility problem I am not familiar with. It is not a parent-child relationship since...
3
1460
by: Yves Dhondt | last post by:
Hello, I've got the following UML design : C | A _____|______ B So 2 objects A and B are connected through a relation C. (For example an employment scheme : person A1 worked for company B1 at time C1, person
1
2277
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual ~A() ;
55
4699
by: Steve Jorgensen | last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of multiple parameters in a Property Let procedure to pass dimensional arguments on the basis that, although it works, it's not obvious how the code works if you don't know the intricacies of the Property Let/Get syntax. Likewise, I dislike (and code to minimize the use of) the VB/VBA syntax of returning a value by referring to the function name as if it were a...
6
2126
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
6
1773
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that the individual elements in an array of ImageList's could be identified by the ID, thereby allowing re-ordering the array without harm. A person could identify by index into the array, but that would not be preserved by re-ordering (and re-ordering...
6
2146
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often times when I program, I will create objects for a specific purpose for a program and if I need to add to it I just add the code.
29
2243
by: Brad Pears | last post by:
Here is a simple OO design question... I have a Contract class. The user can either save an existing contract or they start off fresh with a blank contract, fill in the data and then save a "new" contract. I have a method in my contract class called "Save" which is called like this... dim oContract as new Contract
8
2242
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address, port number, or both. How should we design the classes to represent these filters? My answer was: class FilterRule {
0
9800
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,...
0
11185
Oralloy
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...
0
10778
jinu1996
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...
0
9597
agi2029
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...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5824
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...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4247
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
bsmnconsultancy
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...

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.