I've realised I've written substantially the same code now in four
different projects of mine, so I've decided to wrap it up in a CPANnable
class.

I'm looking for a name to call it, though. Since I find code 20 times
easier to understand than written-word descriptions, consider the
following example use, where, for now, we'll call the class "Banana":

  my $monitorfactory = Banana->new(
                          Prefix  => "MyApp::Monitor",
                          Scalar => "TYPE"
                       );

  ...

  my %args = some_config_reading_here_maybe();

  my $monitor = $monitorfactory->newInstance( "apple", %args );

At this point, the factory will use Module::Pluggable to find every
module within MyApp::Monitor, and 'require' them all. Then it will do
something like

   my $class;
   foreach( @classes ) {
      $class = $_, last if( ${$pkg."::TYPE"} eq "apple" );
   }

   return $class->new( %args );

This being a specific example, there could be something much more
generic, like:

  my $loaderfactory = Banana->new(
                         Prefix => "MyApp::Loader",
                         Test  => sub {
                            my ( $class, $file ) = @_;
                            return $class->canLoad( $file );
                         }
                      );

  open( my $file, "<", $filename ) or die ...;
  my $loader = $loaderfactory->newInstance( $file, $file );

And we'd pass in the filehandle as the key, and each loader implementation
could read magic bytes from the headers, or whatever, and tell the
Banana if it can read the file, so it can pick a supported implementation.

The best name I can come up with is

  Class::SomethingFactory

Does anyone have any better ideas? I'm not sure of the "Something" part
there, it doesn't really convey the generic "magic"ness of the code. But
I can't think up anything else....

-- 
Paul "LeoNerd" Evans

[EMAIL PROTECTED]
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/

Reply via email to