[Chicago-talk] Object question - subclass or make two similar classes

Steven Lembark lembark at wrkhors.com
Sun Feb 1 11:41:39 CST 2004



-- Jay Strauss <me at heyjay.com>

> Hi,
>
> I have 2 classes that have identical methods.  Should I write 2 classes,
> or a parent and 2 subclasses?  The class is "brokerage" as in Ameritrade
> and InteractiveBrokers.  They both have methods like: connect, placeOrder,
> getQuote, getTransactionHistory... but the code to perform these actions
> is totally different between classes.
>
> I'm just wondering, what do I gain by making a single parent, and
> subclassing?

What you probably want is a generic (a.k.a. 'virtual') base
class called "brokerage" that defines the generic methods.
That puts most of your boilerplate in one place.

The derived classes can then supply their own methods to
modify behavior in the base class if necessary. Careful
use of data in the derived classes will probably leave the
base class doing all the work, or at least most of it.

For example, supplying callbacks in the class data might
allow a generic "place an order" method to deal with the
variety between brokerage houses.

Advantages are not having to hack the code in multiple
places to fix/extend it and less work the next time you
have to add a brokerage (i.e., reusability).




--
Steven Lembark                               2930 W. Palmer
Workhorse Computing                       Chicago, IL 60647
                                            +1 888 359 3508



More information about the Chicago-talk mailing list