in reply to Re^3: Law of Demeter and Class::DBI
in thread Law of Demeter and Class::DBI
Let's say we want to indicate that a logger object should flush itself to disk when it's appropriate (it may take some time, so we can't do it right away). An example of not so good OO design:
#in a time sensitive loop $logger->isFlushPending(1); #later on if($logger->isFlushPending) { $logger->flush(); $logger->isFlushPending(0); }
It's bad OO design to use getters to read the state of an object and then use that information to make decisions about how to use the object. The logic controlling the object is now located outside of the object rather than inside it.
So you avoid the getters (asking the object what state it has) and instead tell the object what you want it to do and it can sort out how to do that itself.
#in a time sensitive loop $logger->setFlushPending(); #later on $logger->flushPending();
and the logic controlling whether to flush the log data to disk is now implemented in the flushPending() method. Inside the object, not outside.
See also: TellDontAsk
/J
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Law of Demeter and Class::DBI
by fergal (Chaplain) on Nov 20, 2004 at 00:51 UTC | |
by jplindstrom (Monsignor) on Nov 20, 2004 at 12:34 UTC | |
|
Re^5: Law of Demeter and Class::DBI
by diotalevi (Canon) on Nov 19, 2004 at 22:22 UTC | |
by fergal (Chaplain) on Nov 20, 2004 at 00:52 UTC |