Hi Stevan,
Am 11.04.2009 um 15:22 schrieb Stevan Little:
Rolf,
It is generally considered bad form to use ->new() as your factory
constructor because most people expect that:
Foo->new
Will create a Foo object and not a Bar or Baz or some other
instance. Obviously this can be done just as easily without Moose,
but here is how I would probably go about this:
package PW::PDF::Calendar::Factory;
use Moose;
use List::MoreUtils qw[ any ];
has 'subclass_prefix' => (
is => 'ro',
isa => 'Str',
required => 1,
);
has 'available_subclasses' => (
is => 'ro',
isa => 'ArrayRef[Str]',
required => 1,
);
sub create_calendar {
my ($self, @args) = @_;
my $subclass = $self->_determine_subclass(@args);
confess "Subclass ($subclass) is not one of the available
calendar subclasses"
unless any { $subclass eq $_ } @{ $self-
>available_subclasses };
my $class_name = join '::' => $self->subclass_prefix, $subclass;
return $class_name->new;
}
Then you could easily use it like this:
my $f = PW::PDF::Calendar::Factory->new(
subclass_prefix => 'PW::PDF::Calendar',
available_subclasses => [ qw[ Foo Bar Baz ] ],
);
my $c = $f->create_calendar( ... )
Of course, there is more than one way to do it, but perhaps this
might help you view it in a more Moosey way.
- Stevan
yes, I think I'll di it that way, thanks.
Something like IOC or BreadBoard is to static for this purpose, but
maybe I can use that to solve another problem....
On Apr 11, 2009, at 3:44 AM, Rolf Schaufelberger wrote:
Hi,
Am 10.04.2009 um 22:35 schrieb Stevan Little:
Rolf,
No, there is no one specific Moosey way to do the Factory pattern
in Moose. In fact Moose opens up many different possible ways to
do the Factory pattern depending upon your needs. If you could be
more specific we might be able to provide more suggestions.
- Stevan
So to describe more what I'm doing now in traditional perl oo style:
I have a webshop where you can configure calendars etc of different
kinds with different options.
With the data stored in the database I' have to build a PDF for
printing. Depending on the options stored in the database I
choose the classes to build the PDF dynamically.
So I have my product_item object (instantiated from the
database) which just calls
my $pdf = PW:::PDF::Calendar->new( ...args) ;
$pdf->build();
and in PW::PDF::Calendar I'm looking at the data for this
calendar and choose the correct subclass for this,
so I do in new() (PSEUDOCODE)
my $subclass = get_it some_how_from_args();
...
return PW::PDF::Calendar::$subclass"->new (..args ..)
In old OO style it just works by returning something else from
new(), yet in Moose I just have BUILD which is called after the
object
is created.
On Apr 10, 2009, at 2:08 PM, Rolf Schaufelberger wrote:
Hi,
I'm just trying to get my feet wet with Moose.
The code I woud like to migrate to Moose uses the factory
pattern in several classes. So, is there a Moose-way to
implement a factory class ?
Rolf Schaufelberger
Mt freundlchen Grüßen
Rolf Schaufelberger
plusW GmbH
Stuttgarter Str. 26 Tel. 07183 30 21 36
73635 Rudersberg Fax 07183 30 21 85
www.plusw.de
www.mypixler.com
www.calendrino.de
Mt freundlchen Grüßen
Rolf Schaufelberger
plusW GmbH
Stuttgarter Str. 26 Tel. 07183 30 21 36
73635 Rudersberg Fax 07183 30 21 85
www.plusw.de
www.mypixler.com
www.calendrino.de