Re: Internal State vs. Navigating Associations
- From: "Daniel T." <daniel_t@xxxxxxxxxxxxx>
- Date: Thu, 21 Sep 2006 14:34:07 GMT
"Matt McGill" <matt.mcgill@xxxxxxxxx> wrote:
As a newcomer to OO design, I've been wrestling lately trying to wrap
my mind around the "Tell, Don't Ask" principle and the law of Demeter
for methods. I have been trying to avoid query/decision operations of
this nature:
Bar bar = foo.getFrozzledBar();
if (bar.isFranted()) // do something else
under the assumption (here I am trying to apply "Tell, Don't Ask") that
in this example foo's internal state (bar) is being exposed. Instead I
try to replace the above with a single, more meaningful method on foo,
containing the conditional in some form.
The above does break the LoD but doesn't necessarally break "Tell, Don't
Ask", that all depends on what "do something else" actually does.
Actually, it might not even break LoD, depending on if bar is a newly
constructed object or not.
The question this has prompted me to ask: in what situations is bar a
part of foo's internal state, and in what situations is it really just
an object to which foo has an association?
Obviously this isn't a cut and dried type question. I will say this
though, that object is part of the internal state of *something* and
that thing is the only thing that should be messing with it. So if it is
just an association, then what is it part of the internal state of?
If bar *is* just an
associated object, is it then OK to navigate the association in the
above fashion?
If bar is just an associated object, then it shouldn't be necessary to
navigate the association in the above fashion. :-)
Quick review just to make sure we are on the same page...
"Tell don't Ask" means you should not have to query the state of an
object to see if you have permission to send a particular message to
that object.
"Law of Demeter" means you should only be changing the state of objects
by calling methods on the object whos state you want to change.
--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer have anyone to discuss your doubts with,
nor any ability to discuss them.
.
- References:
- Internal State vs. Navigating Associations
- From: Matt McGill
- Internal State vs. Navigating Associations
- Prev by Date: Re: Is this too much OOP?
- Next by Date: Re: Is this too much OOP?
- Previous by thread: Re: Internal State vs. Navigating Associations
- Next by thread: Re: Internal State vs. Navigating Associations
- Index(es):
Relevant Pages
|