[Tutor] Dependencies among objects

alan.gauld@bt.com alan.gauld@bt.com
Thu, 25 Apr 2002 17:48:16 +0100


> while working on my pet project, I find it increasingly 
> difficult to manage dependencies across objects.

OK, thats strange since OO is supposed to tackle that very 
problem. This suggests your design may be using objects 
but not actually be very object oriented...?

Have you defined your program in terms of a small
(comparitively) set of abstract classes which you can 
subclass to get the concrete behaviour? This normally 
means that the amount of object passing between 
instances is limited to a subset of the asbstract 
objects - and certainly rarely more than 4 or 5.

> Some classes defined objects that are used throughout 
> the app (a page index and a full-text index for instance). 

OK, In that case put them in a module and create 
"global variables" for them.

If you want to be OO pure then make them attributes of 
your application class. Then pass a reference to the 
Application to the other objets.

> Passing around lost of references as arguments is getting 
> messy. 

You should only need to pass a reference to the container 
of your shared objects - usually the Application object itself.

> thought about this design: storing references to helper 
> objects in a parent class so that every child class can 
> access it easily.

What does this buy you over simply setting the helper in 
the constructor for each class?

> class BaseObject:
>      pass
> 
> class Foo(BaseObject):
>      """Helper class used by several
>      other classes throughout the app."""
> 
>      def __init__(self):
>          # Do initialisation stuff and register self
>          # in the parent class.
>          BaseObject.foo = self

What does this give -  if you already have a Foo instance 
you have access to Foo anyway.

> class Bar(BaseObject):
  class Bar(Foo):
>      def doIt(self):
>          value = "I am Bar and I use '%s'."
>          return value % BaseObject.foo.getValue()
           return value % self.getValue()

> Do you think this is a good design?

I guess you realize I'm going to say no... ;-)

I think maybe theres either something I'm missing about
your proposal, or you are missing in the way you're using 
the objects.

Alan G.