cleverett has asked for the wisdom of the Perl Monks concerning the following question:

I want to write a mod_perl application that would allow a user to search, sort and browse through a set of records from a database table represented by a subclass of Class::DBI::Pager

I do this often, so I want to encapsulate all that searching, sorting and browsing logic in it's own class with a few customization hooks:

With any luck, I can write a complete web application like this:

package Physemp::Admin::Clients::Browse; use base 'My::Browse'; ## My::Browse is the base browsing class __PACKAGE__->pager_class('Physemp::Model::Account'); sub items_per_page { return 10; } sub search_template_name { return 'path/to/search/template'; } sub result_template_name { return 'path/to/result/template'; } sub wrapper_template_name { return 'path/to/wrapper/template'; } ## whatever other hooks I need 1; __END__

What I'm not really clear on is how to code the pager_class() method I used just above. If I understand properly, the I need the same effect from that as if I had typed

$class = 'Physemp::Model::Account'; require "$class";

in My/Browse.pm.

Perhaps I'm off and a better way to do that exists. I don't consider myself as up on design patterns as I could be, but I think some people call what I'm talking about a factory class.

TIA for any advice y'all give me.

Replies are listed 'Best First'.
Re: OO Design Advice Sought
by eric256 (Parson) on Nov 18, 2003 at 22:46 UTC

    In a similar case i have used ..

    my $temp = $class; $temp =~ s/::/\\/igs; require "$temp.pm"; $instance = $class->new(@args);

    I'm not sure its error proof but it works like it should.


    ___________
    Eric Hodges
      I've used this in places too.

      I was worried about scoping issues: $class gets set in one package, and then gets used/requires in another package.

        You could then just make this class inherit from that base class, and overwrite a _create sub to do the class initialization

        use Physemp::Model::Account; sub _create { my $self = shift; $self->{instance} = new Phsemp::Model::Account; }

        Then you could do intialization in both the base class and the derived class, and you don't have to worry about scoping. Or you could combine the two together so that you can pass it the name of the class to create.


        ___________
        Eric Hodges