Factories and Interfaces

TheNomad on 2006-06-20T13:20:53

Recently, needing to implement a factory pattern, I started using Class::Factory. It is a nice, easy-to-use base class for factory classes.

But what of the objects produced by my new, nice and shiny factory class, what are they like?

This is important because, if I am going to use a factory, then I need to know that objects produced by it respond to methods in a predictable way. They need the same, or almost the same, interface.

But of course, it's my factory class, and they are my objects, so I will write them all so that they conform to the interface.

The question is, is that enough? Should the factory classes check that any classes registered with them conform to the required interface?

And if I think I need this, should I do it with Class::Roles?


Class::Role(s) are kinda stale

Stevan on 2006-06-20T16:27:10

I would suggest either using Class::Trait, Perl6::Roles or Moose (more specifically Moose::Role). Both Class::Role and Class::Roles are fairly old, and not very robust. But to be honest,.. Roles might actually be overkill for what you are doing.

But if you just want to define an interface, and not any implementation, you could use Class::Interfaces. It makes it very easy to make a Java style interface without needing to make a bunch of .pm files.

- Stevan