[Tutor] Classes + inheritance

Joseph J. Strout joe@strout.net
Fri, 16 Apr 1999 09:53:29 -0700

At 9:32 AM -0700 04/16/99, Arne Mueller wrote:

>1. in module m1 class a creates object of class b (also defined in
>module m1)
>2. in module m2 I'd like to extend/redefine class b, so that class a in
>module m1
>   uses the redefined version (of module m2).

If you want class a to do something different, you must either
(extend/redefine (i.e. subclass) class a, or you must pass it some
information that tells it to do something different.

Your solution does the latter, and it's a perfectly reasonable solution.
Other solutions (drawing from Design Patterns):

1. Factory Method: give m1.a a create() method that creates an m1.b object.
But if you decide to change this behavior from m2, then do it by
instantiating not an m1.a object, but an m2.a object.  Class m2.a differs
from m1.a in only one way: its create() method creates objects of class
m2.b rather than m1.b.


2. Prototype: have your a.create() method take a prototype of the object to
create, and do its work by cloning that object.  Then you can have it
create anything by passing in an example.  This is very similar to your
solution (and certainly no better in this case).

Or, there may be a way to restructure your program altogether so that a
does not have to create any b's.  Without knowing more about your
application, it's hard to say.

-- Joe
|    Joseph J. Strout           Biocomputing -- The Salk Institute |
|    joe@strout.net             http://www.strout.net              |