|
Class::Delegate - easy-to-use implementation of object delegation. |
Class::Delegate - easy-to-use implementation of object delegation.
require Class::Delegate;
@ISA = 'Class::Delegate';
$self->add_delegate('some_name', $a);
$self->add_delegate($b);
$self->do_something_that_b_knows_how_to_do();
$self->do_something_that_a_knows_how_to_do();
This class provides transparent support for object delegation. For more information on delegation, see Design Patterns by Erich Gamma, et al.
delegate() method for information on the usefulness of naming a
delegate).
delegate($name)
If a delegate's class defines a package variable called @PUBLIC, then
it is taken to be a list of method names that are available to be
made visible through the owner object. Otherwise, all methods that
are implemented by the delegate (as returned by can()) will be
available as call-throughs from the owner.
If the delegate object implements a set_owner() method, then that
method will be called as part of the call to add_delegate(). Example:
package Dispatcher;
require Class::Delegate;
@ISA = qw(Class::Delegate);
require Worker;
sub new
{
my ($class) = @_;
my $self = bless {}, $class;
$worker = Worker->new;
$self->add_delegate('gofer', $worker);
}
sub respond_to_error { die "Oh no!\n" }
...
package Worker;
sub new { bless {}, shift }
sub set_owner
{
my ($self, $owner) = @_;
$$self{owner} = $owner;
}
sub do_something
{
my ($self, @args) = @_;
if (!@args) {
$$self{owner}->respond_to_error();
}
...
}
This class only works with owner objects that are implemented as hash references.
If you assign a new value to a named delegate, the Right Thing will not happen.
Kurt D. Starsinic <kstar@cpan.org>
Copyright (c) 2000, Smith Renaud, Inc. This program is free software; you may distribute it and/or modify it under the same terms as Perl itself.
perl(1).
|
Class::Delegate - easy-to-use implementation of object delegation. |