The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

class design & interfaces

I have a base class CBase (no instantiated), two derived classes CDer1 and CDer2, and I also have a pure abstract interface class CInterface. Both CDer1 and CDer2 need to inherit from the interface class.

Which of the following is better?

a) Have CBase inherit from the interface class and give access to the interface to the derived classes through itself

b) Have the derived classes each inherit from the interface class themselves without getting CBase involved.

If you pick a), a follow-up question is: what to do with the interface class methods inside CBase assuming there is no overlap between what the derived classes need to do with these methods. There is no default behavior. Do I just keep these methods as pure abstract in CBase and implement them in the derived classes?

Thanks
C++
Saturday, February 25, 2006
 
 
Without knowing what CInterface is, or how closely related to CBase, CDer1 and CDer2, I say, go with B.

Why? While CDer1 and CDer2 may require CInterface, I doubt CDer3 or CDer(n) will need CInterface.  So, the proposal "A" is unnecessarily hijacking the CBase class family.

In addition, I suspect, the reason you need to separate out a set of behaviors in CInterface, is that not all CBase family share CInterface.  Else, you wouldn't have thought about CInterface.

Example:
CInterface = IDispensable
CBase = CStaff
CDer1 = CProgrammer implements IDispensable
CDer2 = CTester implements IDispensable
:
CDer3 = CManager <-- not dispensable so you don't implement IDispensable.

So, you know CManages won't in toBeOutSourcedList:List<IDispensable>

PS, if you want to prefix classes (rightly or wrongly) with "C", don't use "C" for interfaces, use "I".

At least in C++ which makes no distintion between abstract and interface classes, prefixing "I" will hint future programmers that only pure virtual functions belong here.  Whereelse, people would start adding non-pure virtual function then evolve to "Abstract" from being "Interface".
RM
Sunday, February 26, 2006
 
 
What's the logical connection between CBase and CInterface?  If nothing, then you are adding a non-sensical inheritence relation just to save typing a few characters?  Inheritence models IS-A behavior.  Is that the case here?
bmm6o Send private email
Sunday, February 26, 2006
 
 
Another vote for solution B.

The base class does not actually implement (and thus respect) the interface, and therefore should not inherit from the interface.
Ed
Sunday, February 26, 2006
 
 
I tend to agree with those who support B, but it would be helpful to know about what you're trying to model in more concrete terms.
clcr
Sunday, February 26, 2006
 
 
Without know more, I'd have to vote for solution B as well. I'd try to avoid multiple inheritance in C++ whenever possible, even if from a pure abstract class, because inheriting from a pure abstract class in C++ is definitely *not* the same nor as simple as implementing an interface in Java.
Kyle Send private email
Sunday, February 26, 2006
 
 
Another vote for B if other classes derived from CBase which are not required to implement Interface may exist
Andrei Lopatenko Send private email
Monday, February 27, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz