[Tutor] Deleting an instance of an object

Jeff Shannon jeff at ccvcorp.com
Thu Sep 23 19:56:50 CEST 2004


Adam Cripps wrote:

>(Article is a subclass of Issue, which is a subclass of Title, which
>is a subclass of MagazineCollection). Each instance of Article has an
>attribute name (article.name).
>  
>

I haven't looked closely at your code, but this strikes me as not being 
a very good design.

Remember, inheritance (subclassing) should normally be expressing an "is 
a type of" relationship.  For instance, a "shape" class might have a 
subclass called "square", because a square is a type of shape.  You seem 
to be using subclassing to express an entirely different sort of 
relationship -- a "contains" relationship.  That relationship is a poor 
fit for inheritance -- in this scheme, each and every Article instance 
is also a full MagazineCollection instance.

Rather than using subclassing, you may want to use a technique called 
aggreggation.  Instead of having Title be a subclass of 
MagazineCollection, you should have MagazineCollection hold a list (or a 
dict) of Titles.  You can give MagazineCollection a set of methods for 
adding and removing Title instances from its internal collection, 
getting information about contained Titles, etc.  Similarly, each Title 
should hold a list (or dict) of Issues, and each Issue a list or dict of 
Articles.

Hmmm... glancing at your code a bit more, it *does* look like you have 
internal collections of each subclass in the next-higher class, so it 
seems that you've got that idea down.  So why are you subclassing?   It 
looks like Article instances aren't using any of the MagazineCollection 
code or the Issue code (and sharing code is the main benefit of 
subclassing), so it seems to me that this provides no benefit...   You 
can probably make each subclass a fully independent class, with no 
subclassing involved, without requiring any significant changes to your 
code. 

Jeff Shannon
Technician/Programmer
Credit International




More information about the Tutor mailing list