I've been starting in with Class::MethodMaker and have some questions about get_set for an object.
Let's say I have an class that takes a few inputs, can be instructed to perform various computations, and produces various outputs. To be concrete, say the class is "sum", which takes two arguments, "a" and "b", and sums them to produce "c". (The real object has more inputs, does a much more complicated and expensive computation, and caches its result, but this toy example is good enough to illustrate my question.)
Using Class::MethodMaker, I declare a new_hash_init for the constructor, and "a", "b", "c" as get_set attributes.
After creating an instance (specifying "a" and "b"), I then tell the object to compute, which populates "c".
This approach feels wrong to me.... "c" should be read-only; the object should recalculate "c" whenever "a" or "b" is changed (so the internal state is consistent), etc.
If I were building the object by hand, I'd write set and get to ensure the object stays consistent. But MethodMaker offers only set_get as an accessor/mutator pair... To get the behavior I want, I'd need to override the methodmaker canned procedures, so much so it doesn't seem to make sense to use it.
- Should I be using a different design?
- Should I not be using Class::MethodMaker?
- What is the best design for a situation like this? Don't most objects need to adjust their internal state when a set is used to change one of their attributes?
Thanks for any advice ---
sigma
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.