Qiang has asked for the wisdom of the Perl Monks concerning the following question:
i am trying to design an API to expose some commonly used methods from our internal modules. this api will be used in our webapps.
in the following example. i would like to provide a single access point and chain methods through the wrapper class. i.e $self->api->do_this and $self->api->do_that..
here is something i am uncertain about.. every web app will have a database handler and i would like to reuse it in the company::api. hence i am using it when initializing company::api and passing it down to every modules that need it. i feel clumsy about it.
being a design pattern newbie, i find it difficult to achieve a clean design. i would like to hear other monks opinion on this design..
btw, read some articles on design pattern and this example seems to match the object composition.. I better get my pattern design book real soon.. :)
package company::api; use company::logging; use company::user; sub new { my $class = shift; my $dbh = shift; my $uname = shift; my $self = {}; $self->{__dbh} = $dbh; $self->{__log} = company::logging->new($self->{__dbh}); $self->{__user} = company::user->new($self->{__dbh}, $uname ); bless $self, $class; } sub log_event { my ($self, $log) = @_; $self->{__log}->log_event($log); } sub uid { my $self = shift; unless ( $self->{__uid} ) { $self->{__uid} = $self->{__user}->get_uuid(); } return $self->{__uid}; } ##### usage from webapp package MyWebApp::Base; sub api { unless ($self->{__api}) { $self->{__api} = company::api->new( MyWebApp::DB->dbh, $ENV{RE +MOTE_USER} ); } return $self->{__api}; } package MyWebApp::ControllerOne use base 'MyApp::Base'; sub testing { my $uid = $self->api->uid; $self->api->log_event("$uid is testing"); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: question on OO design and object composition
by samtregar (Abbot) on Mar 20, 2008 at 19:33 UTC | |
by Qiang (Friar) on Mar 22, 2008 at 02:33 UTC | |
by samtregar (Abbot) on Mar 22, 2008 at 06:59 UTC | |
by Qiang (Friar) on Mar 23, 2008 at 22:12 UTC | |
|
Re: question on OO design and object composition
by Herkum (Parson) on Mar 21, 2008 at 05:13 UTC |