Not quite sure what you mean by blindly here.

I meant putting an accessor/mutator on every field, or even a large ammount of fields, without putting thought into it.

As for making them public, the whole point of encapsulation is to hide implementation with an interface. Thus why should I make them public.

No, encapsulation is the reason why you should avoid them in the first place, or (perhaps better) make them private or protected.

I don't know where you came up with this idea, that a mutator may only set a member variable directly without processing, but you need to leave that definition behind.

No, that's the accepted definition in most OO lituature. If your method does something besides direct access, it's not an accessor or mutator.

. . . we have a mutator (temperature or as I prefer setTemperature, but to each his own) that will accept temperatures in either celcius, farenheit, and perhaps other scales as well . . . If we took your approach then a farenheit temperature may be stored into the object, and then calculations further down the chain will get really messed up. So the solution to your method would be to create a conversion routine that you would have to pass the foreign value through and then pass the celcius temp back to the mutator.

I was thinking about just such a situation a few days ago, and thought up this solution: the setTemperature (or whatever you call it) method should take an object of type Temperature. The Temperature class provides methods like get_celsius and get_farenheit which do all the conversions for you. Likewise, its constructor would take temperatures in various scales. Methods that set and return complex objects are no more accessors/mutators then those that are doing the complexity themselves, so this method can't be considered a use of accessors/mutators.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated


In reply to Re^9: OO Getters/Setters by hardburn
in thread OO Getters/Setters by theAcolyte

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.