[Israel.pm] An OOP Pattern Discovered by Accident

Shlomi Fish shlomif at iglu.org.il
Sat Jan 27 13:04:39 PST 2007


On Saturday 27 January 2007 18:40, Oded Arbel wrote:
> On Sat, 2007-01-27 at 05:07 +0200, guy keren wrote:
> > it would be nice if programming languages supported the "delegate"
> > keyword, to allow stating "methods A, B and C should be delegated to
> > object O". i've often found the need for such code (where inheritance
> > was not appropriate for the delegation), and having some 10 methods
> > which just invoke the next object both requires extra coding, and makes
> > it less obvious that this is a simple delegation.
>
> Well, some programming languages do support better infrastructure for
> this pattern - for example, C#. Other systems support this pattern with
> automated code generation (as I've mentioned before) - like Eclipse for
> Java.
>

In a way, Perl 5 also supports this pattern:

<<<<<<<<<<<<<<<<<
#!/usr/bin/perl 

use strict;
use warnings;

package MyClass;

use base 'Class::Accessor';

sub hello
{
    print "Hello World!\n";
}

sub add
{
    my ($self, $x, $y) = @_;

    return $x+$y;
}

sub print_add
{
    my ($self, $x, $y) = @_;

    printf "Hello %i!\n", $self->add($x,$y);
}

package DelegateTo;

use base 'Class::Accessor';

__PACKAGE__->mk_accessors(qw(obj));

sub new
{
    my $class = shift;
    my $self = $class->SUPER::new();

    $self->obj(MyClass->new());

    return $self;
}

# The magic is here:
{
    no strict 'refs';
    foreach my $method (qw(hello add print_add))
    {
       
        *{__PACKAGE__."::".$method} =
            sub { 
                my $self = shift; 
                return $self->obj()->$method(@_); 
            };
    }
}
# End of magic.

package main;

my $d = DelegateTo->new();

# methods are delegated to $d->obj()

$d->print_add(10, 30);

$d->hello();

print "3 + 4 = ", $d->add(3,4), "\n";
>>>>>>>>>>>>>>>

The code inside the magic can be abstracted into a specialised class method 
that does this. I suppose Lisp and other dynamic languages also support this.

> Eh. That's the one thing I'm missing when comparing perl to Java :
> automated refactoring.

Well, there's a difference between automated refactoring and automated code 
generation, which is what you've mentioned before. You can automatically 
generate Perl code if that's what you need, but automatically refactoring it 
is not possible at the moment, except for some basic stuff using 
Devel::Refactor. Moreover, the refactorings provided by Eclipse and other 
Java IDEs are only a subset of the refactorings covered in the "Refactoring" 
book by Martin Fowler.

Regards,

	Shlomi Fish

>
> --
> Oded
>
> ::..
>
> Indecision is the true basis for flexibility.
>
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl

-- 

---------------------------------------------------------------------
Shlomi Fish      shlomif at iglu.org.il
Homepage:        http://www.shlomifish.org/

Chuck Norris wrote a complete Perl 6 implementation in a day but then
destroyed all evidence with his bare hands, so no one will know his secrets.



More information about the Perl mailing list