Module Design and Implementation
In this example, the CGI::MailForm class proxies calls to the param() method in the
contained CGI object:
package CGI::MailForm;
use CGI;
# basic constructor
sub new {
my $pkg = shift;
my $self = { query => CGI >new() };
bless($self, $pkg);
}
# the proxying param method
sub param {
my $self = shift;
return $self >{query} >param(@_);
}
1;
Contrast this with an implementation using inheritance:
package CGI::MailForm;
use CGI;
@ISA = qw(CGI);
In both cases users of the module can use the param() method:
my $mailForm = CGI::MailForm >new();
$mailForm >param(foo => bar );
Composition also has the advantage of scaling better than inheritance. An
object that contains five other objects of various types is no harder to write than
one that contains one object. In contrast, doing five way multiple inheritance is
probably grounds for commission to a mental facility.
If you are going to use inheritance, keep it simple. Having worked on a Perl
project with an inheritance hierarchy over six levels deep, I can safely state that
here be dragons . Deep inheritance hierarchies make understanding the behavior
of a particular class very difficult either documentation is repeated at each level,
or readers must perform a tedious search process up the inheritance tree to find
the method they're looking for.
83
83
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved