[Tutor] Design - getFoo and setFoo methods

Alexandre Ratti alex@gabuzomeu.net
Mon, 20 May 2002 15:56:47 +0200


At 17:41 16/05/2002 +0100, alan.gauld@bt.com wrote:
> > When do you use getFoo() and setFoo() methods in your
> > classes?

[Direct access of class attributes:]
>Thats a common Python idiom but if you are accessing too
>many attributes directly it suggests the class is missing
>some methods. Theres an OOP expression for this called the
>Law of deMeter if you want more research material - try the
>Cetus Links page.

OK, I found these definitions:

"The Law of Demeter was originally formulated as a style rule  for 
designing object-oriented systems. "Only talk to your immediate friends" is 
the motto."

"A more general formulation of the Law of Demeter is: Each unit should have 
only limited knowledge about other units: only units "closely" related to 
the current unit. Or: Each unit should only talk to its friends; Don't talk 
to strangers."

http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/general-formulation.html

Or:

"As a guideline, it's simply "where possible, avoid middlemen"."

http://c2.com/cgi/wiki?LawOfDemeter

=> So there is a kind of tension between two goals:
- Allow direct access to attributes to avoid unnecessary setFoo/getFoo 
methods ;
- But avoid creating tight coupling of distant objects through direct 
access of attributes (toto = foo.bar.baz.crunch).


Cheers.

Alexandre