Qiang has asked for the wisdom of the Perl Monks concerning the following question:
following are a simple verison and i would like to get some opinion from someone with more OO experience.
here is how I will call my utility methods in CGI::Application controller. i like it this way because i will have access to the utility methods by simple $self->sis->method call.
here is a wrapper module to all the other utility modules:package MyApp::ControllerOne; use base 'MyApp::Base'; $self->sis->add_event("event name"); $self->sis->log_event("message"); $self->sis->uuid(); package MyApp::Base; use base 'CGI::Application'; sub sis { my $self = shift; unless ( $self->{__sis} ) { $self->{__sis} = Company::Dept->new($dbh, $uname); } return $self->{__sis}; }
here is utility module:package Company::Dept; sub new { my $class = shift; my $self = {}; my ($dbh, $uname) = @_; $self->{ __dbh } = $dbh; $self->{ __uname } $uname; bless ($self, $class); return $self; } sub log_event { my ($self, $msg) = @_ unless ( $self->{__elog} ) { $self->{__elog} = Company::Dept::EventLogging->new( $self->dbh +, $self->uuid ) ; } $self->{__elog}->log_event( $msg ); } sub add_event { my ($self, $msg) = @_ unless ( $self->{__elog} ) { $self->{__elog} = Company::Dept::EventLogging->new($self->dbh, + $self->uuid ) ; } $self->{__elog}->add_event( $msg ); } sub uuid { my $self = shift; unless ( $self->{__uuid} ) { $self->{__uuid} = Company::Dept::UUID->uuid($self->dbh, $uuid +) ; } return $self->{__uuid} ; } sub dbh { return $self->{__dbh} } sub uname {return $self->{__uname} }
UPDATE: fixed the sub sis.package Company::Dept::EventLogging; sub add_event { } sub log_event { } package Company::Dept::AnotherUtilityModule; sub aaa { } sub bbb {}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: OO code reuse in CGI::Application
by Narveson (Chaplain) on Mar 17, 2008 at 18:15 UTC | |
by Qiang (Friar) on Mar 17, 2008 at 18:36 UTC |