Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: OOP: Plugin Style Development

by Jenda (Abbot)
on Jul 22, 2002 at 20:34 UTC ( #184229=note: print w/ replies, xml ) Need Help??


in reply to OOP: Plugin Style Development

I think that instead of a "base" class that will be overriden to support another media it will be better to create a class that, during creation, accepts a "helper" class that implements the I/O scheme.

package ShoppingCart; sub new { my $class = shift; my $self = {}; bless $self, $class; $self->initialize(@_); return $self; } sub initialize { my $self = shift; $self->{IO} = shift; } sub PrintAll { my $self = shift; while (my $item = $self->{IO}->GetNext()) { ... } } ... package ShoppingCart::IO::File; # this is NOT a subclass of ShoppingCart!!! sub new { ... } sub GetNext {} sub ResetCounter {} ... package ShoppingCart::IO::DBI; sub new { ... } sub GetNext {} sub ResetCounter {} ... package main; my $IO = new ShoppingCart::IO::DBI $server, $username, $password; my $Cart = new ShoppingCart $IO; ...

There might be some inheritance between the IO Scheme implementation objects, but I doubt it.

Also ... if you really really cared you could add some code to ShoppingCart::initilize() that would test whether the passed IOScheme object supports all the required methods with $IO->can(...).

I do not know if this will be of any help in your case though.

  Jenda


Comment on Re: OOP: Plugin Style Development
Download Code
Re: Re: OOP: Plugin Style Development
by dragonchild (Archbishop) on Jul 22, 2002 at 21:19 UTC
    That is what he's doing. We're discussing how to implement the Helper pattern. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      I'm not 100% it IS what is he doing. It doesn't hurt to explain it in more detail for others even if he does.

      Anyway here's some code that implements the method checking:

      # in package ShoppingCart @required_methods = qw(GetNext GetFirst Reset ...); sub initialize { my $self = shift; my $IO = shift; croak "The first parameter to 'new ShoppingCart' must be an IO objec +t!" unless ref $IO; foreach my $method (@required_methods) { croak("The IO object passed to 'new ShoppingCart' does not impleme +nt the necessary methods!") unless $IO->can($method); } $self->{IO} = $IO; } ...

      The test is not compile time, but currently it's the best you can get. AFAIK of course.

        Jenda

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://184229]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-12-28 06:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (179 votes), past polls