Plinking Away Part the Second

In my last post I identified three MooseX modules that might help me out and end the perhaps some tedious typing and bring a more structure design into my ADD game. The first of this is MooseX::Abstract::Factory.

Despite its name it does not actually create 'Abstract' classes in the sense of a class that cannot be instantiated like a 'Java Abstract Class'. I guess what they mean is a 'Factory that is Abstract' i.e. not tied to any one class or name-space.

Despite this little bit of confusion on the part of a grizzled Java guy, this MooseX is does have possibilities for me. It lets you create an instance of a class from withing a name-space. On the down side the documentation is sparse and the test suite does not really do more that what is in the POD so you can't look for any neat examples in there. But then again the concept is simple and it is a very common and did not take long for me to apply to my Character instance.

So to start I create a new 'Factory' class to create new characters and all it consists of is;


package RPG::ADD::Character::Factory;

use MooseX::AbstractFactory;

implementation_does [ qw( RPG::ADD::Character::Ability::Henchmen
RPG::ADD::Character::Ability::OpenDoorsOnA

) ];
implementation_class_via sub { 'RPG::ADD::Class::' . shift };

The 'implementation_does' allows me to add in all the Roles that every Character starts with, I just used two by way of example, if you have time and want to see them all have a look here.

The 'implementation_class_via' allows me to specify the name-space I will be building from. Now I need only change my little create_fighter.pl to use the factory
create command with the 'Class' I am invoking and a hash-ref of attributes.


use RPG::ADD::Character::Factory;

my $fighter = RPG::ADD::Character::Factory->create('Fighter',
{name=>'Sir Cumferace',
strength=>18,
exceptional=>91},
);

print $fighter->name()."\n";
print "strength=".$fighter->strength();
print " (".$fighter->exceptional().")\n"
if($fighter->can('exceptional'));
print "opens a door on a ". $fighter->open_door_on_a()."\n";
print "opens a barred door on a ".$fighter->open_barred_doors_on_a
if($fighter->can('open_barred_doors_on_a'));

and give it a go


Sir Cumferace
strength=18 (91)
opens a door on a 4
opens a barred door on a 1

and I get the same results as I had going way back to this post.

Now what does this give me??

Well it does clean up my base 'Character' class now I do not have to put all the roles in that class I can have them in my Factory.

It does get rid of this annoyance


has 'intelligence' =>(
	is		=>'rw',
	isa		=>'Int',
	default=>0,
);
 with qw( RPG::ADD::Languages );   

of having to put the with after the attribute to stop the role from complaining it has no 'intelligence' when the object is instantiated.

So I do get some good traction out of it but I am not 100% sold on it yet. I will have to look at the other two before and compare before I make up my mind

Field_dressing.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations