24oop

23
1 Object Oriented Programming (OOP)

Upload: muttuswami

Post on 10-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 1/23

1

Object Oriented Programming

(OOP)

Page 2: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 2/23

2

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 runslower than one written in a language without objects(based on the assumption that abstraction away fromthe 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.

Page 3: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 3/23

3

Key Concepts and Terminology

• OOP encapsulates data (attributes) and subroutines(behavior) into packages called "objects"

• The software instantiation of an object is typically calleda "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 "functionoriented" – 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 inBioPerl

Page 4: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 4/23

4

Key Concepts and Terminology

• Fundamental principle of good softwareengineering – 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'sinterface is unchanged

• Java to DB direct access VSJava/Perl/PHP VS Webservices example

Page 5: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 5/23

5

Example in Perl

(Class/package)sub Cow::speak { ## here I'm separating "Cow" (the package) from theprint "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

Page 6: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 6/23

6

Method Invocation Arrow

• "class" is a group of things with similar behaviors and traits

Class->method• invokes subroutine method in package

Class

Page 7: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 7/23

7

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

Page 8: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 8/23

8

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 likeinterpolation???)

Cannot do this:

$beast::speak;

Page 9: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 9/23

9

Separating the Package

from the Methodsub 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;}

Page 10: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 10/23

10

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

Page 11: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 11/23

11

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 noarguments are passed

Page 12: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 12/23

12

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

Page 13: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 13/23

13

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"

Page 14: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 14/23

14

Inheritance Example

Rather than duplicate the "speak" subroutine for eachanimal, make a common subroutine package calledAnimal:

{ package Animal;sub speak {

my $class = shift;print "a $class goes ", $class->sound, "\n";

}

}

For each animal, you can say it "inherits" from Animal

Page 15: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 15/23

15

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 it5) 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.

Page 16: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 16/23

16

#!/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

Page 17: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 17/23

17

A slightly more sophisticated

example

Page 18: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 18/23

18

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 inthis fashion, so that if we later change how the data is stored users of this 'interface' can accessthe data in the same way.

#simple keys -- don't need quotes

sub organism { $_[0] -> {_organism} }

sub chromosome { $_[0] -> {_chromosome} }

sub pdbref { $_[0] -> {_pdbref} }

1;

Page 19: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 19/23

19

Frankly -- the previous example is onereason people sometimes do not like perl.

The way it is written 'obfuscates' what isgoing on.

Page 20: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 20/23

20

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 Gene1return(\%_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;

Page 21: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 21/23

21

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";

Page 22: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 22/23

22

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";

Page 23: 24OOP

8/8/2019 24OOP

http://slidepdf.com/reader/full/24oop 23/23

23

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";