Kurt D. Starsinic > Class-Delegate-0.06 > Class::Delegate

Download:
Class-Delegate-0.06.tar.gz

Dependencies

Annotate this POD

Related Modules

Test::More
Crypt::CBC
more...
By perlmonks.org
View/Report Bugs
Module Version: 0.06   Source  

NAME ^

Class::Delegate - easy-to-use implementation of object delegation.

SYNOPSIS ^

    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();

DESCRIPTION ^

This class provides transparent support for object delegation. For more information on delegation, see Design Patterns by Erich Gamma, et al.

METHODS ^

add_delegate([ $name, ] $delegate)

Assigns a delegate to your object. Any delegate can be named or unnamed (see the delegate() method for information on the usefulness of naming a delegate).

resolve($methodname, $delegatename)

Declare that calls to $methodname should be dispatched to the delegate named $delegatename. This is primarily for resolving ambiguities when an object may have multiple delegates, more than one of which implements the same method.

delegate($name)

This method returns the delegate named $name, or the empty list if there is no such delegate.

SOME DETAILS ^

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.

EXAMPLES ^

CALLING THE OWNER FROM THE DELEGATE

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();
        }

        ...
    }

BUGS ^

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.

AUTHOR ^

    Kurt D. Starsinic <kstar@cpan.org>

COPYRIGHT ^

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.

SEE ALSO ^

perl(1).

syntax highlighting: