Object Oriented Programming (OOP)
Object Oriented Programming (OOP)
(OOP)
1
Object Oriented Programming
(OOP)
• OOP Pros
– programs will take less time to read, write, debug, and
maintain
– helps for managing large programming projects
(especially with multiple coders)
• OOP Cons
– "generally" a program in an OO language will run
slower than one written in a language without objects
(based on the assumption that abstraction away from
the machine code is slower).
– "C++ programs have worse instruction cache locality
(than C)"
• Quantifying Behavior Differences Between C and C++
Programs. Calder, Grunwald, Zorn. Journal of Programming2
Languages, Vol2, Num 4, 1994.
Key Concepts and Terminology
• OOP encapsulates data (attributes) and subroutines
(behavior) into packages called "objects"
• The software instantiation of an object is typically called
a "class"
• data and subroutines are intimately coupled
• Objects have the property of "information hiding"
– objects do not generally know how each other are implemented
– communicate via well-defined "interface"
– analogy: drive car without knowledge of how transmission works
• Programming is "object oriented" as opposed to "function
oriented"
– we have been "function oriented" programming
– will continue to be for the rest of the class, however, a basic
understanding of OOP allows us to access modules available in
BioPerl
3
Key Concepts and Terminology
• Fundamental principle of good software
engineering
– separate interface from implementation
• Makes it easier to modify programs
– changing a class's implementation does not
affect the "client" as long as the class's
interface is unchanged
• Java to DB direct access VS
Java/Perl/PHP VS Webservices example
4
Example in Perl
(Class/package)
sub Cow::speak { ## here I'm separating "Cow" (the package) from the
print "a Cow goes moo\n"; ## "speak" (the subroutine) with "::"
}
sub Horse::speak {
print "a Horse goes neigh\n";
}
sub Sheep::speak {
print "a Sheep goes baaah\n";
}
Cow::speak;
Horse::speak;
Sheep::speak;
#a Cow goes moo
#a Horse goes neigh
#a Sheep goes baaah
5
Method Invocation Arrow
• "class" is a group of things with similar
behaviors and traits
Class->method
• invokes subroutine method in package
Class
6
Method Invocation Arrow
sub Cow::speak {
print "a Cow goes moo\n";
}
sub Horse::speak {
print "a Horse goes neigh\n";
}
sub Sheep::speak {
print "a Sheep goes baaah\n";
}
Cow->speak;
Horse->speak;
Sheep->speak;
#a Cow goes moo
#a Horse goes neigh
#a Sheep goes baaah
7
Parts are Separable NOW
Allows us to do this:
my $beast = "Cow";
$beast->speak;
$beast::speak;
8
Separating the Package
from the Method
sub Cow::speak {
print "a Cow goes moo\n";
}
sub Horse::speak {
print "a Horse goes neigh\n";
}
sub Sheep::speak {
print "a Sheep goes baaah\n";
}
9
Observations
• Excessive common code
– each speak subroutine has similar structure:
• a print and a string that is mostly similar
• OOP feature
– minimize common code
– write in once
– test and debug only once
10
Method Invocation
with Extra Parameter
The invocation of:
Class->method(@args)
really does this:
Class->method("Class",@args)
meaning you get the class name as the first
parameter or the only parameter if no
arguments are passed
11
Rewrite Animal Methods
sub Cow::speak {
my $class = shift;
print "a Cow goes moo\n";
}
sub Horse::speak {
my $class = shift;
print "a Horse goes neigh\n";
}
sub Sheep::speak {
my $class = shift;
print "a Sheep goes baaah\n";
}
Why bother????
12
Second Method to Simplify
{ package Cow;
sub sound { "moo"}
sub speak {
my $class = shift;
print "a $class goes", $class->sound,"\n";
}
}
13
Inheritance Example
Rather than duplicate the "speak" subroutine for each
animal, make a common subroutine package called
Animal:
{ package Animal;
sub speak {
my $class = shift;
print "a $class goes ", $class->sound, "\n";
}
}
14
Putting it All Together
{ package Animal;
sub speak {
my $class = shift;
print "a $class goes ", $class->sound, "\n";
}
}
{ package Cow;
@ISA = qw(Animal);
sub sound {"mooo"}
}
Cow->speak;
Note – the excessive curly braces are to emphasize that there are no requirement for symbols to be in the same file or
scope. See example next slide.
In many ways -- the explicit passing "information" between code, subroutines and packages is rather appealing for
understanding OOP and inheritance.
15
#!/usr/bin/perl
package Animal;
sub speak {
my $class = shift;
print "a $class goes ",$class->sound,"\n";
}
package Cow;
@ISA = qw(Animal);
sub sound { "moo"}
package Horse;
@ISA = qw(Animal);
sub sound { "neigh"}
package Sheep;
@ISA = qw(Animal);
sub sound { "baah"}
Cow->speak;
Horse->speak;
Sheep->speak;
16
A slightly more sophisticated
example
17
package Gene1;
use strict;
use warnings;
use Carp; #gives us "croak"
sub new {
my ($class, %arg) = @_; # Note -- %arg is a hash!
return bless # bless REFERENCE, CLASS
{ # general convention UNDERSCORE means internal variable
# Next line -- we access the hash (passed in) -- to assign the value to "this" hash
_name => $arg{name} || croak ("no name"),
_organism => $arg{organism} || croak ("no organism"),
_chromosome => $arg{chromosome} || ("no chromosome"),
_pdbref => $arg{pdbref} || ("no pdbref"),
},$class; #returned hash reference 'marked' with "Gene1"
}
sub name { $_[0] -> {_name} } # the method receives the object (Gene1) as the first parameter
# which in this case is just a reference to a hash. The data is abstracted away in
this fashion, so that if we later change how the data is stored users of this 'interface' can access
the data in the same way.
19
package Gene1; #Gene1.pm
use strict;
use warnings;
use Carp; #gives us "croak"
sub new {
my ($class, %arg) = @_;
# $_hash is an internal hash
my %_hash =();
sub name { $_[0] -> {_name} } # the method receives the object as the first parameter
# which in this case is just a reference to a hash
# The data is abstracted away in this fashion,
# so that if we later change how the data is stored
# users of this 'interface' can access the
# data in the same way.
use Gene1;
21
print "\nOjbect 2:\n\n";
my $obj2 = Gene1->new (
name => "Abca4",
organism => "Mus musculus");
print "\n";
print '"bless" tags the reference with the object/package name'."\n";
print 'print ref $obj1, "\n";'."\n";
print ref $obj1, "\n";
22
print "\nOjbect 3:\n\n";
my $obj3 = Gene1->new (
organism => "Homo Sapiens",
chromosome => "15",
pdbref => "pdb999.ent" );
23