Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!rphroy!caen!kuhub.cc.ukans.edu!maverick.ksu.ksu.edu!ux1.cso.uiuc.edu!uxa.cso.uiuc.edu!dawg6844 From: dawg6844@uxa.cso.uiuc.edu (Dan Walkowski) Newsgroups: comp.lang.smalltalk Subject: Re: To Subclass or Encapsulate? (WAS: Access Methods) Message-ID: <1991Apr24.213502.18364@ux1.cso.uiuc.edu> Date: 24 Apr 91 21:35:02 GMT References: <2815b936.c65@petunia.CalPoly.EDU> Sender: usenet@ux1.cso.uiuc.edu (News) Distribution: usa Organization: University of Illinois at Urbana Lines: 40 mjohnson@cosmos.acs.calpoly.edu (Mark S. Johnson) writes: >I could: > (1) create Vector as a subclass of Array and modify/add methods > (e.g., methods to insure that the elements of a Vector were > only Numbers, vector multiplication methods, etc) >-OR- > > (2) create Vector as a subclass of Object and "encapsulate" an > Array inside Vector as an instance variable. That way I could > more easily protect the underlying Array from messages that I > don't want it to receive. >-OR- These questions are most definitely not too naive for this group. These are important questions, which unfortunately do not have easy answers. Once again, it depends on the exact situation. In other words, what are you going to DO with your vectors once you have them? Depending on the answer to that question, I could make arguments for either answer (1), (2), or (3). For example, if you are going to do lots of 'array-like' stuff with your vectors, like moving, sorting, and otherwise diddling with individual elements, then I would probably argue for case number (1). If, however, you want to think of your vectors more like 'whole' objects, instead of a collection of elements, (or if a vector 'contains' other information besides its elements, for example: a length? a color? who knows?) then I would personally choose implementation (2). And, lastly, if the 'main' thing you wish to do with your vectors were something completely different, say plot them in different colors, then perhaps it should be subclassed from some graphics object. (?) Believe me, I almost ALWAYS spend more time deciding how to structure my problem than actually writing the code. Why? Because it makes the code cleaner and easier to write, and easier to change later. dan -- _______________________________________________________________________________ Dan Walkowski | To understand recursion, Univ. of Illinois, Dept. of Comp. Sci. | you must first understand recursion. walkowsk@cs.uiuc.edu |