Re^9: Assignable Subroutines
by hardburn (Abbot) on Jan 26, 2005 at 14:14 UTC
|
Why isn't your salary in the database? Why aren't you changing it there?
If your object is simply an access layer above the database (Class::DBI or something along those lines), then the mutator is no longer trivial.
"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.
| [reply] |
|
|
| [reply] |
|
|
| [reply] |
|
|
|
|
Maybe it is in the DB but I'm not changing it in the database because the part of my program that wants to mutate the Employee doesn't (and shouldn't) need to know about databases. Also, storing it in a DB does not always mean that the mutator handles updating in the DB. If persistence is handled by a framework outside the object then the mutator remains trivial and storage is handled outside of your classes.
Basically it comes down to the fact that many objects have chunks of them that behave exactly like a record/struct in that they are nothing more than passive fields that get twiddled by outsiders. These plain old fields should still not be part of your public interface because if they need to be something more than just plain old fields in the future you're shafted (unless you're in a language that makes it possible for an object to intercept direct twiddling of it's fields and do fancy stuff - Python's properties for example).
If you don't provide mutators, even for trivial fields, then you are just storing up pain for the future.
| [reply] |
|
|
If you don't provide mutators, even for trivial fields, then you are just storing up pain for the future.
No, you fix your design so you don't need them in the first place.
In this case, I would make an "effect" object (I'm sure there is a Pattern for this). An effect stores values that can mutate other values when applied to another object.
my $employee = Employee->new( salary => 50_000 );
my $effect = Effect->new;
$effect->set( salary => 1.02, '*' ); # A 2% raise
$effect->set( frobnitz => 1.5, '+' ); # The frobnitz is up
$employee->apply_effect( $effect );
The key here is you don't need to know for certain that a given field is implememented in Employee. The apply_effect method ignores any fields it doesn't recognize (like frobnitz). I'll give you that it is more lines of code than:
$employee->salary( $employee->salary * 1.02 );
But it is also a superior form of encapsulation.
"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| [reply] |
|
|