Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Object Oriented Programming (OOP). Exam Friday Paper questions –6 Subroutines BLAT/BLAST Hash Modules Sorting 2.

Similar presentations


Presentation on theme: "1 Object Oriented Programming (OOP). Exam Friday Paper questions –6 Subroutines BLAT/BLAST Hash Modules Sorting 2."— Presentation transcript:

1 1 Object Oriented Programming (OOP)

2 Exam Friday Paper questions –6 Subroutines BLAT/BLAST Hash Modules Sorting 2

3 3 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 Programming Languages, Vol2, Num 4, 1994.

4 4 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

5 5 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

6 6 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 ## Nothing new here

7 7 Method Invocation Arrow "class" is a group of things with similar behaviors and traits Class->method invokes subroutine method in package Class

8 8 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 Functionally appears to be NO different, but it is

9 9 Parts are Separable NOW Allows us to do this: my $beast = "Cow"; $beast->speak; Package name is separated from the subroutine name Can use a variable package name (sort of like interpolation???) Cannot do this: $beast::speak;

10 10 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"; } my @farm = qw(Cow Cow Horse Sheep Sheep); foreach my $beast (@farm) { $beast->speak; }

11 11 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

12 12 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

13 13 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????

14 14 Second Method to Simplify { package Cow; sub sound { "moo"} sub speak { my $class = shift; print "a $class goes", $class->sound,"\n"; } # can do same for Horse, and Sheep But only the package name, and specific sound change share the definition for "speak" between the Cow, Horse, and Sheep – with "inheritance"

15 15 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"; } For each animal, you can say it "inherits" from Animal

16 16 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; 1)Perl constructs argument list (just "Cow") 2)Perl looks for Cow::speak (not there) 3)Perl checks for the inheritance array @Cow::ISA It exists and contains "Animal". This is a way for "sharing" methods. Note that we've factored out the speak subroutine from all the animals. 4)Perl checks for "speak" inside "Animal" (Animal::speak) and finds it 5)Perl invokes subroutine as if: Animal::speak("Cow"); 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.

17 17 #!/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; # a Cow goes moo # a Horse goes neigh # a Sheep goes baah

18 18 A slightly more sophisticated example

19 19 bless bless REF,CLASSNAME bless REF This function tells the thingy referenced by REF that "it" is now an object in the CLASSNAME package. If CLASSNAME is omitted, the current package is used. Because a "bless" is often the last thing in a constructor, it returns the reference for con- venience. Always use the two-argument version if the function doing the blessing might be inherited by a derived class. See perltoot and perlobj for more about the blessing (and bless- ings) of objects.

20 20 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. #simple keys -- don't need quotes sub organism { $_[0] -> {_organism} } sub chromosome { $_[0] -> {_chromosome} } sub pdbref { $_[0] -> {_pdbref} } 1;

21 21 Frankly -- the previous example is one reason people sometimes do not like perl. The way it is written 'obfuscates' what is going on.

22 22 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 =(); $_hash{_name} = $arg{name} || croak("No name"); $_hash{_organism} = $arg{organism} || croak("No organism"); $_hash{_chromosome} = $arg{chromosome} || "No chromosome"; $_hash{_pdbref} = $arg{pdbref} || "No PDB reference"; bless \%_hash,$class; #hash is 'marked' with Gene1 return(\%_hash); # return a reference to a 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. sub organism { $_[0] -> {_organism} } sub chromosome { $_[0] -> {_chromosome} } sub pdbref { $_[0] -> {_pdbref} } print "hellow world\n"; 1;

23 23 use strict; #testGene1.pl use warnings; use Gene1; print "Ojbect 1:\n\n"; my $obj1 = Gene1->new ( ## passing a hash into a subroutine!!! name => "BBS4", organism => "Homo sapiens", chromosome => "15", pdbref => "pdb999.ent" ); print $obj1->name, "\n"; print $obj1->organism, "\n"; print $obj1->chromosome, "\n"; print $obj1->pdbref, "\n";

24 24 print "\nOjbect 2:\n\n"; my $obj2 = Gene1->new ( name => "Abca4", organism => "Mus musculus"); print $obj2->name, "\n"; print $obj2->organism, "\n"; print $obj2->chromosome, "\n"; print $obj2->pdbref, "\n"; print "\n"; print '"bless" tags the reference with the object/package name'."\n"; print 'print ref $obj1, "\n";'."\n"; print ref $obj1, "\n";

25 25 print "\nOjbect 3:\n\n"; my $obj3 = Gene1->new ( organism => "Homo Sapiens", chromosome => "15", pdbref => "pdb999.ent" ); print $obj3->name, "\n"; print $obj3->organism, "\n"; print $obj3->chromosome, "\n"; print $obj3->pdbref, "\n";


Download ppt "1 Object Oriented Programming (OOP). Exam Friday Paper questions –6 Subroutines BLAT/BLAST Hash Modules Sorting 2."

Similar presentations


Ads by Google