Well factored Objects

goran.hultgren at bluefish.se goran.hultgren at bluefish.se
Wed Apr 30 08:55:49 UTC 2003


Hi again Jimmie!

Jimmie Houchin <jhouchin at texoma.net> wrote:
[SNIP]
> I've read much theory, but have never applied any. I knew that when I 
> got there, that is where the real learning would happen.
> 
> I was just concerned I might be doing bad OO if my Person object had 
> dozens or 100+ instance variables. So what I should do is decompose into 

Yes, that is a correct "fear".

> smaller objects which properly encapsulate their data/behaviour and 
> aggregate those objects into the Person object but create many, many 
> instance methods in the Person object which handle the interface to all 
> of its sub-objects.

Well, perhaps not handle "all" - just as long as you are aware of the
dependency implications.
 
> Am I gaining any understanding here, am I making progress. :)
> Or do I still miss something?

I think you are gaining. Let me also give some "general" object hunting
advice:

1. Make sure that you can come up with behaviours for your objects. If
you can't then perhaps it is not a good object and it may simply be
attributes more properly kept in a "larger" parent object - even if this
means a few more instance variables.

2. Be also very suspicious of objects without instance variables! :-)
Yes, it may sound silly, but as a newbie it is sometimes a trap easy to
fall into.

3. Analyze the life cycles of the objects. For example, if a "part"
object of your JHPerson has the exact same life cycle as JHPerson and
can't be used on its own *nor* be reused in any other context - then the
arguments for keeping it a separate object are getting drastically
reduced. In Richard's design with multiple name objects being *related*
to a JHPerson the name objects do *not* share the life cycle (not same
birth and death) of the JHPerson and that makes them much more "likely"
to be "good objects".

4. Consider reuse. Not necessarily between apps but also inside your
application. If an object is more likely to appear in another context it
is a good argument to have it as an object "of its own".


Well, these are a few of them. There are of course tons of more
heuristics. The life cycle heuristic is often forgotten though IMHO.

> And again much thanks for educating this newbie.
> 
> Jimmie Houchin

No problem - since it's on the list hopefully others also can gain.

regards, Göran



More information about the Squeak-dev mailing list