I certainly wouldn't say inheritance was evil, but it's not always the best solution.

In the context of Net::LDAP, it might be helpful to consider the various reasons people might subclass or encapsulate LDAP connections, and how those might work together.

For example, let's say that people have created several subclasses of Net::LDAP:
- one like yours, which adds some convenient high-level methods;
- one that better caches results to avoid repeated requests to the LDAP host; and
- one that proxies the LDAP requests over SOAP to a mod_perl server behind a firewall, where the LDAP server is located.

Ideally, I'd like to be able to get an object that has the Express interface, the Caching functionality, and the SOAPProxy implementation, but if they're all just simple subclasses of Net::LDAP, it's awkward to assemble such a beast. (Cf. "diamond pattern".) Perhaps you could accomplish it using mixin classes, but still, ick.

If each of those packages is implemented as a "decorator" or "proxy," using delegation, then things suddenly become simpler:

my $ldap_handle = Net::LDAP::Express->new( debug => 0, target => Net::LDAP::Caching->new( duration => 30, target => Net::LDAP::SOAPProxy->new( soap_url => '...' ) ) );

This kind of mix-and-match composition of functionality provides a combinatorial explosion of possibilities, rather than the incremental addition of a subclass.


In reply to Re: Inheritance vs Delegation: pros and cons by simonm
in thread Inheritance vs Delegation: pros and cons by bronto

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.