473,670 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is adding public methods to base classes bad design?

Hi All,
Sorry if this is off topic, but I could not seem to find a suitable OO
Design newsgroup. If there is one feel free to let me know.

Here is a simplification of a general design problem I face.

Say you have:

class Car {
virtual void speedup();
virtual void slowdown();
};

class BondCar {
virtual void self_destruct() ;
};

Now storing a std::list<Car *> and calling speedup/slowdown is fine, however
if you wanted to self_destruct all BondCars in the list I can think of a few
ways to do it:

1) Use dynamic_cast, this is obviously not a good solution for more complex
designs.
2) Bring self_destruct down into the base class, making a 'fat' interface
3) Storing two lists, one std::list<Car *> and the other std::list<BondC ar
*>, but then if I want to speed up all cars I have to iterate over two lists

None of these solutions seems very well suited to more complex designs, and
I have come across this problem over and over again. As it says in the c++
faq lite, inheritance is for old code to call new code, and NOT for code
reuse, so it almost seems to me that adding new public methods to derived
classes is wrong.

Could someone please enlighten me?
Andy
Jul 19 '05 #1
5 2426

Andrew Ward <an***@ihug.co. nz> wrote in message
news:EP******** **************@ news.xtra.co.nz ...
Hi All,
Sorry if this is off topic, but I could not seem to find a suitable OO
Design newsgroup. If there is one feel free to let me know.
comp.object
Here is a simplification of a general design problem I face.

Say you have:

class Car {
virtual void speedup();
virtual void slowdown();
};

class BondCar {
virtual void self_destruct() ;
};

Now storing a std::list<Car *> and calling speedup/slowdown is fine,
No it isn't. Those members are private.
however
if you wanted to self_destruct all BondCars in the list I can think of a few ways to do it:

1) Use dynamic_cast, this is obviously not a good solution for more complex designs.
I always wince at having any dynamic_cast, but it's still the best of a bad
choice of solutions in some cases.
2) Bring self_destruct down into the base class, making a 'fat' interface
If 'self_destruct' makes sense for all Cars, then it should be in Car. If it
only makes sense for BondCars, then it should only be in BondCar. I don't
think its location should be dictated by your application without regard for
its proper place. I don't have a problem with "fat" interfaces. In fact, the
more abstract the class in which you can put a member, the better, as long
as the member makes sense in that class.
3) Storing two lists, one std::list<Car *> and the other std::list<BondC ar
*>, but then if I want to speed up all cars I have to iterate over two lists

A variation on 3) is to have one list with all Cars, including BondCars, and
another with just BondCars. It's harder to delete a BondCar, as it has to be
removed from both lists, but you don't need any downcasts and you don't need
to put members in the wrong class.

It's impossible to choose without knowing your application better.
None of these solutions seems very well suited to more complex designs, and I have come across this problem over and over again. As it says in the c++
faq lite, inheritance is for old code to call new code, and NOT for code
reuse, so it almost seems to me that adding new public methods to derived
classes is wrong.
I thought it was to represent IS-A relationships, but maybe I'm behind the
times.

There's nothing wrong with adding anything to anything as long as it solves
your problem, makes sense, and doesn't cause a maintenance nightmare.
Could someone please enlighten me?


That's about the best I can do.

DW

Jul 19 '05 #2

"Andrew Ward" <an***@ihug.co. nz> wrote in message
news:EP******** **************@ news.xtra.co.nz ...
Hi All,
Sorry if this is off topic, but I could not seem to find a suitable OO
Design newsgroup. If there is one feel free to let me know.

Here is a simplification of a general design problem I face.

Say you have:

class Car {
virtual void speedup();
virtual void slowdown();
};

class BondCar {
virtual void self_destruct() ;
};
I'm assuming those are supposed to be public functions.
Now storing a std::list<Car *> and calling speedup/slowdown is fine, however if you wanted to self_destruct all BondCars in the list I can think of a few ways to do it:
First, I think you should take a step back and ask yourself about the
implications of a design where you would even consider having a list of base
class objects, and then attempting to do subclass functions on only some of
those objects. This isn't a trivial subject.
None of these solutions seems very well suited to more complex designs, and I have come across this problem over and over again. As it says in the c++
faq lite, inheritance is for old code to call new code, and NOT for code
reuse, so it almost seems to me that adding new public methods to derived
classes is wrong.


Generally speaking, I can't imagine why public methods in subclasses could
be wrong. Inheritance is used often, even when there is no such thing as
"old" code and "new" code. i.e. it's used right in the original design.
Anyway, I don't think you have a fundamental OO problem with inheritance, I
think you have a specific design issue related to lists of things. You can
do more reading in this area. The C++ FAQs book by Cline is a short start
(is bag-of-apple a kind-of bag-of-fruit?, etc.)
Jul 19 '05 #3
"Andrew Ward" <an***@ihug.co. nz> wrote in message
news:EP******** **************@ news.xtra.co.nz ...
Hi All,
Sorry if this is off topic, but I could not seem to find a suitable OO
Design newsgroup. If there is one feel free to let me know.

Here is a simplification of a general design problem I face.

Say you have:

class Car {
virtual void speedup();
virtual void slowdown();
};

class BondCar {
virtual void self_destruct() ;
};

Now storing a std::list<Car *> and calling speedup/slowdown is fine, however
if you wanted to self_destruct all BondCars in the list I can think of a few
ways to do it:

1) Use dynamic_cast, this is obviously not a good solution for more complex
designs.
2) Bring self_destruct down into the base class, making a 'fat' interface
3) Storing two lists, one std::list<Car *> and the other std::list<BondC ar
*>, but then if I want to speed up all cars I have to iterate over two lists

None of these solutions seems very well suited to more complex designs, and
I have come across this problem over and over again. As it says in the c++
faq lite, inheritance is for old code to call new code, and NOT for code
reuse, so it almost seems to me that adding new public methods to derived
classes is wrong.

Could someone please enlighten me?
Andy


I've encountered a similar situation where self_destruct() makes sense only
for BondCar. I'm considering introduction of a member which tells me to what
class a specific object belongs. Then I can check the member before applying
self_destruct() to an object. It's not the OO way, my code would look ugly,
but I expect it'll work. Any suggestion please?

--
ES Kim
Jul 19 '05 #4

"ES Kim" <es***@svd.co.k r> wrote in message
news:bi******** **@news1.kornet .net...
"Andrew Ward" <an***@ihug.co. nz> wrote in message
news:EP******** **************@ news.xtra.co.nz ...
Hi All,
Sorry if this is off topic, but I could not seem to find a suitable OO
Design newsgroup. If there is one feel free to let me know.

Here is a simplification of a general design problem I face.

Say you have:

class Car {
virtual void speedup();
virtual void slowdown();
};

class BondCar {
virtual void self_destruct() ;
};

Now storing a std::list<Car *> and calling speedup/slowdown is fine, however if you wanted to self_destruct all BondCars in the list I can think of a few ways to do it:

1) Use dynamic_cast, this is obviously not a good solution for more complex designs.
2) Bring self_destruct down into the base class, making a 'fat' interface 3) Storing two lists, one std::list<Car *> and the other std::list<BondC ar *>, but then if I want to speed up all cars I have to iterate over two lists
None of these solutions seems very well suited to more complex designs, and I have come across this problem over and over again. As it says in the c++ faq lite, inheritance is for old code to call new code, and NOT for code
reuse, so it almost seems to me that adding new public methods to derived classes is wrong.

Could someone please enlighten me?
Andy
I've encountered a similar situation where self_destruct() makes sense

only for BondCar. I'm considering introduction of a member which tells me to what class a specific object belongs. Then I can check the member before applying self_destruct() to an object. It's not the OO way, my code would look ugly, but I expect it'll work. Any suggestion please?


It would be better to use dynamic_cast.

john
Jul 19 '05 #5
"ak" <ak @ workmail.com> wrote in message
news:ra******** *************** *********@4ax.c om...
On Fri, 22 Aug 2003 14:01:13 +1200, "Andrew Ward" <an***@ihug.co. nz> wrote:
|1) Use dynamic_cast, this is obviously not a good solution for more complex |designs.
|2) Bring self_destruct down into the base class, making a 'fat' interface
|3) Storing two lists, one std::list<Car *> and the other std::list<BondC ar |*>, but then if I want to speed up all cars I have to iterate over two lists |
|None of these solutions seems very well suited to more complex designs, and |I have come across this problem over and over again. As it says in the c++ |faq lite, inheritance is for old code to call new code, and NOT for code
|reuse, so it almost seems to me that adding new public methods to derived
|classes is wrong.
I would go for (2), having a virtual self_destruct() in base class which does nothing but implemented in the derived class.


So where you have:
pCar->self_destruct( );

the car will refuse unless it's a BondCar? A self-destruct that leaves the
car intact.

DW

Jul 19 '05 #6

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

Similar topics

19
2333
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
175
8776
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
3
5758
by: MIGUEL | last post by:
Hi all, I'm quite lost with how adding web references to a project creates proxy classes. I've developed a web service with two classes inside and that contains three references to three DLLs. After building it up, I've deployed it on the web server. From my Windows application, I then add a web reference to that web service.
34
3689
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm thinking of making all these pimpl class declarations public. Reasoning is that since only the code within the ..cc file will need to ever access them, why protect them in ways that would make access to them more difficult and obfuscated? What...
86
4609
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere."
11
18112
by: Pete Kane | last post by:
Hi All, does anyone know how to add TabPages of ones own classes at design time ? ideally when adding a new TabControl it would contain tab pages of my own classes, I know you can achieve this with ListView columns so it should be doable, thanks
12
1594
by: Bob Jones | last post by:
I have an odd business requirement and I think that the implementation is not correct in the terms of OOP development. Any help on the concepts would be very appreciated! We currently have a custom Page object which is derived from the base Page object. We also have custom controls that derive from a base class that performs custom drawing and inherits from our own IOurControl interface. There is also a special caching layer in the mix...
23
2634
by: Chris Gordon-Smith | last post by:
Hello All I have a base class called Action_Request, and a set of classes corresponding to different kinds of Action_Request, each of which inherits from Action_Request. Eg:- class Add_Molecule_Req: public Action_Request{ // ...... };
2
1777
by: fgh.vbn.rty | last post by:
Hi, I'm not sure if i'm asking the question correctly but anyway here it is. Say I have 3 classes - class A, class B, class R. 1) A and B are the building blocks and R is like a repository that stores objects of A and B. 2) A is at the lowest level and should "know about" only other As. B should know only about As and other Bs.
0
8388
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
8817
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...
1
8593
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,...
0
8663
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5687
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
4215
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...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1799
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.