Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to get a container-component proxy thing?

by flowdy (Scribe)
on Apr 07, 2014 at 19:44 UTC ( [id://1081434]=perlquestion: print w/replies, xml ) Need Help??

flowdy has asked for the wisdom of the Perl Monks concerning the following question:

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1081434]
Approved by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found