Hi,

Moose provides a powerful means of delegation one can use, the handles parameter to 'has'. The problem I see with it while planning my software architecture is that the object shall not have full and general access to the object it delegates to, so a direct link to the delegate I consider something to avoid. Plus, specific to the problem to be solved with the delegated call, private methods could be used at delegate side directly, what reminds of friend keyword in C++, I am not sure.

In my case, the delegating instance is a component, the delegate is a container that links to the said instance in a part-of-me relation. This is a circular reference I would have to weaken in one direction to prevent a potential memory leak, by the way, but this would be no problem.

My made-up plan of implementing this appears to be no better than simple and general delegation at last, even though considering just other aspects, maintainability in the first place, and not to forget scalability. How do experts this kind of pondering fruits?

package Servant::PartlyJustDelegating; has callback_proxy => ( is => 'ro', isa => 'CodeRef', required => 1, ); BEGIN { for (qw( invent_example fail_admittedly )) { no strict 'refs'; my $callback = $_; *$callback = sub { goto &{ shift->callback_proxy->($callback) }; }; } } package Omnipotential::Entity; has servants => ( is => 'ro', isa => 'ArrayRef[Servant::PartlyJustDelegating]', ); has _servant_interface_proxy => ( is => 'ro', isa => 'CodeRef', lazy => 1, builder => '__build_servant_interface_proxy', init_arg => undef, ); sub __build_servant_interface_proxy { my ($self) = @_; weaken($self); my %callback_for = ( invent_example => sub { my $part = shift; ... }, fail_admittedly => sub { my ($part, $arg) = @_; ... }, ); return sub { my $name = shift; return $callback_for{ $name } // croak "callback not found: $name"; }; } sub _build_servant { my ($self, $row) = @_; ... return Servant::PartlyJustDelegating->new( @arguments, callback_proxy => $self->_servant_interface_proxy, ); }

Hope you wise, dear monks can shed me some light on how to do it right. I probably just miss a proper overview on design patterns, that is, the ability to properly comprehend the often toughly abstract description of these.

Thank you for reading (+ replying),
-- flowdy.


In reply to How to get a container-component proxy thing? by flowdy

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.