Perl – Factory Pattern
Mercredi 16 juillet 2008
Pas de Bullshit : je suis une fabrique, tu me donnes des paramètres, je te donne de beaux objets associés.
#!/usr/bin/perl -w
package MyFactory;
sub new {
my $self = shift;
my $type = shift;
my $class = "MyTest::$type";
return $class->new(@_);
}
1;
package MyTest::Repeat;
sub new {
my $class = shift;
my $self = {
text => shift,
repeat => shift,
};
return bless $self, $class;
}
sub display {
my $self = shift;
print ($self->{text} x $self->{repeat});
}
1;
package MyTest::Stamp;
sub new {
my $class = shift;
my $text = shift;
return bless \$text, $class;
}
sub display {
my $text = shift;
my $stamp = localtime();
print "$stamp $$text";
}
1;
package main;
use strict;
my $text_r = MyFactory->new("Repeat", "Hello\n", 3);
$text_r->display();
my $text_stamp = MyFactory->new("Stamp", "Bye-bye\n");
$text_stamp->display();
>./test.pl
Hello
Hello
Hello
Wed Jul 16 10:16:32 2008 Bye-bye
Autres articles susceptibles de vous intéresser :
Tags: Developpement, Pattern, Perl
Posted in App | No Comments »
