Elk or Meta?

I tried to do this in several ways, but none of them look elegant enough. (I also wonder if CPAN or Moose has this. Dozens of searches that I did over time did not show anything, which is consistent.)

I want to create a class type that

  • is Base + Facade + Factory for other classes that load themselves as destination types.
  • "factory" is just Base->new( %params ) , and this creates types based on policies registered by a separate subclass.
  • Each subclass knows the basic things about a base class domain, but I try to keep it minimal. See the example below: UnresolvedPath just knows that we must first check for existence.

An obvious example of this are file directories and files:

 package Path; use Moose; ... sub BUILD { my ( $self, $params ) = @_; my $path = $params->{path}; my $class_name; foreach my $test_sub ( @tests ) { $class_name = $test_sub->( $path ); last if $class_name; } croak "No valid class for $path!" unless defined $class_name; $class_name->BUILD( $self, $params ); } package Folder; use Moose; extends 'Path'; use Path register => selector => sub { -d $_[0] }; sub BUILD { ... } package UnresolvedPath; extends 'Path'; use Path register position => 1, selector => sub { !-e $_[0] }; 
  • Question: Does Moose provide an elegant solution? Or would I have to go into Class :: MOP for the most part?
+6
design perl moose
source share
2 answers

Take a look at http://code2.0beta.co.uk/moose/svn/MooseX-AbstractFactory/ and feel free to steal. (Mine.)

+3
source share

If you really want to make a Builder template or Abstract Factory Sample , then you can do it and nothing will stop you. But maybe you really need a little Inversion of Control / Injection of dependencies ? For this you can check the bread board

+2
source share

All Articles