Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I'm far from an authority on OO, but my first instinct would be to encapsulate the knowledge about what each object needs as far as configuration within the object itself.

Having one object that grabs all the configuration and holds it is a great idea, especially when the storage on disk or eslewhere might change. Having the configuration object know a lot about the insides of the other objects I think violates the encapsulation for which you chose OO in the first place. It's probably best for the configuration object to get and hold the configuration, then make a flexible interface for each other object ask it for what they each need.

I'd pass a reference to the config object into all the other objects. I know that's a pain, but it's what I'd do out of the alternatives. I'd make sure the configuration object had a nice way to get the values out, either as a hash or as individual key/value pairs. Something like:
my %conf_val = %{ $conf->values( 'conf_section' ) };
my $val1 = $conf->value( 'conf_section', 'conf_item' );
The configuration object then doesn't need to know which objects need which configuration items, let alone how they need them. It just needs to be able to provide what is asked of it, and to do it in a predictable way. The other objects don't need to know how the config object gets or stores the values, or even whether it's eagerly or lazily. This is, as I understand it, one of the biggest advantages to using OO.

Of course, it's not likely you'd need two or more configurations in a single running program. In this particular situation, a globally accessible object that returns the values it is asked to return may be a good compromise. It'd surely be ahead of making the configuration object aware of each of the other objects on my list. In fact, making the configuration object's behavior depend on the calling object wouldn't even be on my list.

Christopher E. Stith

In reply to Re: OO Design question: configuration (Encapsulate the knowledge about an object within the object itself) by mr_mischief
in thread OO Design question: configuration by spurperl

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (4)
    As of 2018-04-20 22:52 GMT
    Find Nodes?
      Voting Booth?