Object Oriented Programming (OOP) and introduction to BioPerl Laurent Falquet (original course by Marco Pagni), Basel October, 2006 Swiss Institute of Bioinformatics Swiss EMBnet node Overview in 3 parts � Motivation for OOP � The key concepts of OOP � Using BioPerl objects LF Basel October 2006
Part I Motivation for OOP LF Basel October 2006 Background � OOP didn't come out of � The "software crisis" the blue. It has strong manifests itself in historical roots in other � 1. cost overruns � 2. user dissatisfaction paradigms and practices. w ith the final product � It came about to address � 3. buggy softw are problems commonly � 4. brittle softw are grouped together as the "software crisis". LF Basel October 2006
Complexity � Software is inherently complex � Some factors that impact on because and reflect complexity in software � w e attempt to solve problems in complex � The number of names domains (variables, functions, etc) � w e are forced by the size of that are visible the problem to w ork in � Constraints on the time- teams sequence of operations � softw are is incredibly (real-time constraints) malleable building material � Memory management � discrete systems are prone (garbage collection and to unpredictable behavior address spaces) � softw are systems consist � Concurrency of many pieces, many of � Event driven user w hich communicate. interfaces. LF Basel October 2006 How do humans cope w ith complexity in everyday life? � Humans deal with � To be useful, an complexity by abstracting abstraction (model) must details away. be smaller than what it represents. � E.g., Surfing the Internet doesn't require � E.g., road map vs know ledge of internal photographs of terrain processors registers; vs physical model. sufficient to think of a computer as simple visualization tool. LF Basel October 2006
Exercise 1 � Memorize as many numbers from the following sequence as you can. I'll show them for 30 seconds. Now write them down. � 1759376099873461324287593345108941120765934 � How many did you remember? � How many could you remember with unlimited amounts of time? LF Basel October 2006 Exercise 2 � Write down as many of the following telephone numbers as you can: Home: Cellular: Boss: Co-worker: Parents: Post Office: Fax: Friends 1: Friend 2: Pizza: LF Basel October 2006
Answ er to the Exercises 1 and 2 � By abstracting the details � Working with abstractions of the numbers away and lets us handle more grouping them into a new information. concept ( telephone number ) we have increased our information handling capacity by nearly an order of magnitude! LF Basel October 2006 Exercise 3 � How many of these (unrelated) concepts can you memorize in 30 seconds? LF Basel October 2006
Answ er to the Exercises 3 � Working with abstractions � Miller ( Psychological lets us handle more Review , vol 63(2)): information (e.g. phone � "The Magical Magical Number Number numbers), but we're still Seven, Plus or Minus Seven Tw o: Some Limits on limited by Miller's Our Capacity for observation. Processing Information" � What if you have more than 7 things to juggle in your head simultaneously? LF Basel October 2006 Hierarchy � A common strategy: form � Common examples are a hierarchy to classify and � military, large companies, order our abstractions. administration � Linnaeus’ classification system of organism � EC numbers for enzymatic reactions � UNIX file system. LF Basel October 2006
Decomposition � Divide and conquer is a � Failure in one part, if properly handy skill for many thorny designed, won't cause failure life problems. of the whole. This depends on the issue of coupling. � We want to compose a system from small pieces, rather than � We can beat this grim view of build a large monolithic a system composed of many system, because the former can parts by properly decomposing be made more reliable. and decoupling. Another reason is that we can divide up the work more easily. LF Basel October 2006 Object technology � Nothing unique about � We'll put our abstractions forming abstractions, but into a hierarchy to keep in OOP this is a main them organized and focus of activity and minimize redundancy. organization. � Natural way to "divide � We can take advantage and conquer" the large of the natural human state spaces we face tendency to anthropomorphise. (complexity). � We'll call our abstractions objects. LF Basel October 2006
Part II The Key Concepts of OOP LF Basel October 2006 Class and Instance � A class is a part of a � An instance of an object program that describes the is a member of a class properties of an object. which has received particular values to its These properties fall into two broad categories: attributes. � attributes - the data associated to an object, � methods - the functions or procedures or subroutines that comes along w ith an object. LF Basel October 2006
Class and Instance example � A "square" class could � Two instances of the have size and color square class may consist attributes and methods to in a large blue square and alter them: a tiny red one: Name square Attributes size, color Methods set_size, set_color, perimeter LF Basel October 2006 Encapsulation � The attributes of an object � The methods of an object usually receive some usually receive some degree of privateness. degree of privateness. � Private attributes are � Encapsulation: the not accessible from values of an object's outside the object. attributes should only be altered by its ow n � Public attributes can be methods. Private directly accessed by any attributes should alw ays other objects. be favored. LF Basel October 2006
Encapsulation � A "person" class could � Encapsulation hides the have two attributes, name implementation away and credit card number from the user. for example. � One should be able to drive a car w ithout � Nobody w ant its credit know ing how the engine card number being w orks… public! LF Basel October 2006 Benefit of encapsulation � Encapsulation is a technique for minimizing interdependencies among objects by defining a strict external interface. � This way, internal coding can be changed without affecting the interface, as long as the new implementation supports the same (or upwards compatible) external interface. LF Basel October 2006
Methods � One could broadly distinguish four kinds of methods � A method that creates a new object is called a constructor. � A method that destroys an object is called a destructor. � It is frequent to define many simple access methods just to set or get attribute values. These methods ensure data encapsulation. � Other methods that permit the object to perform some useful actions. LF Basel October 2006 Hierarchy through inheritance � Classes can have children � Inheritance aids in the that is, one class can be reuse of code. created out of another class. Name parent � A sub-class inherits all Attributes familyname the attributes and methods of the super-class, and Name child may have additional Attributes familyname attributes and behaviors. LF Basel October 2006
Hierarchy example � A very simple class diagram: Vehicule Attr: speed Land-Vehicule Air-Vehicule Meth: start Attr: nr-wheel Attr: nr-wing stop Meth: Meth: take-off land Car Bike Plane Attr: Attr: Attr: Meth: fuel Meth: Meth: fuel LF Basel October 2006 A real example of hierarchy � The BioPerl class diagram: LF Basel October 2006
Polymorphism � Polymorphism means the � Think to a computer ability to request that the desktop where one like to same operations be open, resize and close any performed by a wide window, whatever is the range of different objects. content. � Polymorphism is a consequence of inheritance. From previous example, any vehicle can start and stop. LF Basel October 2006 Objects In Perl Are Deceptively Simple � An object instance is � A method is simply a simply a reference that subroutine that expects happens to know which an object reference (or a class it belongs to (a package name, for class reference is a scalar , just methods) as the first like a number or a string). argument. � A class is simply a � A class inherits through package that happens to @ISA array provide methods to deal with object references. LF Basel October 2006
Class vs module package MyObj; package MyMod; sub new { # constructor sub f { … } my $class = ref(shift); # get class ref sub g { … } my $self = { @_ }; # set attributes … bless ($self, $class); # create object 1; return($self); # return instance ref } sub other_methods { … } … 1; LF Basel October 2006 Class vs module use MyObj; use MyMod; … … # call constructor MyMod::f($param); my $instance = MyObj->new($attr); my $a = MyMod::g(); # call method $instance->other_methods(); LF Basel October 2006
Recommend
More recommend