Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

You know that strategy classes are easy to make and use. For example, you want to encapsulate your output formats in such a way that you can select the appropriate wrapper without affecting client code.

package Output_Strategy::HTML; package Output_Strategy::CSV; . . . my $output_strategy = $opt{'html'} ? 'Output_Strategy::HTML' : 'Output_Strategy::CSV'; $output_strategy->preamble; $output_strategy->render(@data); $output_strategy->postamble; # whatever
One of the nice aspects of this technique is that it is run-time dynamic: you can reassign (or re-bless, if the strategy classes don't differ by data) the strategy object ($output_strategy) at any time, to select a different strategy.

Now, this technique makes the following assumption (or, more accurately, imposes the following constraint): the entity to be strategized (output format, in the example above) is a stand-alone class or object. It could even be a delegate within another object.

But, suppose you are extending the capabilities of a class via inheritance rather than delegation/composition. How can you apply a strategy pattern in the inheritance?

My answer is quite simple, and, again, it exploits Perl's highly dynamic nature. It involves modifying the inheritance tree at run time. And to make it more flexible, I insert a level of indirection in the inheritance chain, so that the "end user" classes don't need to be affected.

Suppose we have a set of classes (e.g. Foo) that want to "mix in" the methods of our output interface. Let Foo and Bar inherit from Output_Strategy; and let Output_Strategy inherit — based on a run-time setting — from one of the actual strategy classes.

package Foo; use base 'Output_Strategy'; # this is a 'strategy handle class' . . . # set the inheritance path at run time: @Output_Strategy::ISA = ( $opt{'html'} ? 'Output_Strategy::HTML' : 'Output_Strategy::CSV' );
And any time you need to change the strategy, you don't have to change the inheritance list (@ISA) of the "end user" classes (e.g. Foo).

Note: I'm not saying this is better than delegation. I just think it's a potentially interesting alternative technique.


Update: I have referred to this "double-indirection" of classes as "handles" because the technique reminded me of the double-indirection of memory management, used in some operating systems. In Windows, these are called handles, and it makes sense; it's the same idea as file handles, and in fact (IIUC), Windows handles are an abstraction over all of the system resources a process can access. Wikipedia has an article on Handles, and it says a handle is "an abstract reference to a resource". But that doesn't really describe what I'm doing here, which is more accurately called a bridge. Wikipedia's article on Opaque pointers helpfully/confusingly refers to both handles and the Bridge pattern. :-)

We're building the house of the future together.

In reply to Strategy Handles by jdporter

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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (7)
    As of 2015-11-25 22:59 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (693 votes), past polls