473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 MyNetworkProtocol: 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 1787
<fe***********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.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 MyNetworkProtocol: 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_ptr<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***********@gmail.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 MyNetworkProtocol: 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
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,...
3
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...
1
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...
55
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...
6
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...
6
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...
6
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...
29
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...
8
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,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...

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.