25.03.2013 Views

Day 3 – OOPs, I wrote it again… - Scott Gorlin

Day 3 – OOPs, I wrote it again… - Scott Gorlin

Day 3 – OOPs, I wrote it again… - Scott Gorlin

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>OOPs</strong>, I <strong>wrote</strong> <strong>it</strong> again...<br />

IAP 2009<br />

<strong>Scott</strong> <strong>Gorlin</strong><br />

gorlins@m<strong>it</strong>.edu<br />

http://stellar.m<strong>it</strong>.edu/S/project/advanced-matlab/


Excuse the t<strong>it</strong>le…<br />

• OOP - Object Oriented Programming<br />

• Object is a marriage of:<br />

• Methods <strong>–</strong> functions<br />

• Fields/Properties <strong>–</strong> data


OOP<br />

• In the beginning there was<br />

• Hole code<br />

• Hex code<br />

• Spaghetti code<br />

• Function code<br />

• Object code


• Spaghetti Code<br />

OOP<br />

• Before functions, programs were lists of<br />

commands that sometimes jumped from line to<br />

line<br />

• Debugging was awful, because you had to<br />

unravel the spaghetti trace!


• Function Code<br />

OOP<br />

• Functions, w<strong>it</strong>h lim<strong>it</strong>ed scope, are the ideal<br />

basis for reusable subun<strong>it</strong>s of code<br />

• Many programs/engines exist as a collection of<br />

functions, making design/debugging easier


• Function Code<br />

OOP<br />

• BUT, w<strong>it</strong>h larger/cooperative projects, functions can<br />

intertwine just like spaghetti!<br />

• Simple example <strong>–</strong> loadData(), loadData() , but now what kind of<br />

data did you want to load?<br />

• loadDataFor<strong>Scott</strong>()<br />

• loadMyData()<br />

• loadMyData2()<br />

• cortinaLoadData3()<br />

• loadDataFor<strong>Scott</strong>sExperimentOnNov12_1987_noDisp()


OOP<br />

• Object-Oriented Programming<br />

• Organizes functions by context, relation, and data type<br />

• Goals<br />

• Create larger, more significant, reusable subun<strong>it</strong>s of code<br />

• Isolate and protect all methods related to a kind of data<br />

• Easily reusable collections for doing a given thing<br />

• Concise, related documentation


OOP<br />

• So, What Is An Object?<br />

Terminology<br />

• Terminology<br />

• Class <strong>–</strong> defin<strong>it</strong>ion of a kind of object, along<br />

w<strong>it</strong>h what that object can do<br />

• Object <strong>–</strong> instance of a class, containing unique<br />

data


OOP<br />

• Real world examples<br />

• Class Car (type ( type of variable) variable<br />

• Methods<br />

• drive, turn, park, cruise, fillUp, crash<br />

• Fields<br />

• Owner, Color, Speed, Gas, BatteryLevel<br />

• Objects (variables ( variables)<br />

• myCar = [];<br />

• timsCar.Owner = ‘Tim’, timsCar.Gas = 0.9;


OOP<br />

• Real world examples<br />

• Car method defin<strong>it</strong>ions (pseudocode):<br />

function drive(car, driver):<br />

if driver == car.Owner<br />

car.Speed = 10<br />

else<br />

car.soundAlarm()<br />

if car.hasOnStar()<br />

car.callPolice()


• An Object is:<br />

OOP<br />

• A set of intimately related data and functions<br />

• A Class is:<br />

• The defin<strong>it</strong>ion of those relations


OOP<br />

• Basic Usable Example<br />

• Class for a given data/file type<br />

• Methods<br />

• read(myData), wr<strong>it</strong>e(myData), plot(myData)<br />

• Instead of<br />

• readMyDataType(d), wr<strong>it</strong>eMyDataType(d),<br />

plotMyDataType(d)


OOP<br />

• What is an object in Matlab?<br />

• Actually just a structure w<strong>it</strong>h a different name<br />

• Contains all properties of a structure (has any<br />

set of fields/sizes) w<strong>it</strong>h the add<strong>it</strong>ional benef<strong>it</strong>s:<br />

• Calling a function on that structure calls a class<br />

method instead of the general function on the path,<br />

if <strong>it</strong> exists<br />

• Access to fields strictly controlled by methods<br />

• Dual syntax:<br />

method(obj, args1, arg2, ...)<br />

===<br />

obj.method(arg1, arg2, ...)


• One problem...<br />

OOP<br />

• Syntax has completely changed in 7.6<br />

• Much better, but 7.6 code will not run in prior<br />

versions<br />

• Old classes will run in 7.6+ (for now)<br />

• Old syntax not covered here, but see my slides<br />

from last year


OOP<br />

• How to create a class in Matlab<br />

• Create an mfile named MyClass.m or a folder<br />

named @MyClass<br />

• This folder should be in a path directory, but you do<br />

NOT need to add the folder <strong>it</strong>self to the path!<br />

• Create MyClass.m in the @MyClass folder<br />

• Allows defining methods in new m-files in this<br />

folder, otherwise two methods are identical


OOP Example<br />

• Let’s create a class for a polynomial function<br />

N<br />

∑<br />

i=1<br />

• f(x) =<br />

Ai x<br />

• Finished class is online<br />

B i<br />

• Create m-file Polynomial.m


OOP Example<br />

• MyClass.m must define:<br />

• Class name, class attributes, class inher<strong>it</strong>ance<br />

• Properties of the class<br />

• And property attributes<br />

• Methods for the class<br />

• Alternatively, each method can be defined in<br />

a new m-file in the @class directory


OOP Example


Encapsulation<br />

• Encapsulation<br />

OOP<br />

• Object fields are (usually) PROTECTED<br />

• Cannot read/wr<strong>it</strong>e them from parent function,<br />

only object methods can!<br />

• Works like scope for functions<br />

• We must wr<strong>it</strong>e methods to allow user to modify<br />

object fields (like argument passing and local<br />

variables)


Encapsulation<br />

• Encapsulation<br />

OOP<br />

• ‘Private’ Private’ or 'protected' <strong>it</strong>ems only visible to the<br />

object <strong>it</strong>self<br />

• ‘Public’ Public’ <strong>it</strong>ems visible and/or modifiable from<br />

outside


Encapsulation<br />

• Encapsulation<br />

• Why?<br />

OOP<br />

• Defensive: All property controls go through object<br />

• Can’t wr<strong>it</strong>e ‘3 rd party’ functions that modify data<br />

w<strong>it</strong>hout object knowing <strong>it</strong>!<br />

• Provides only one access path to cr<strong>it</strong>ical data


Encapsulation<br />

• Encapsulation<br />

OOP Example<br />

• Here, A and B are protected<br />

• They must always be synced, ie same shape,<br />

and in the same order<br />

• Now they can never change unless the object<br />

does <strong>it</strong> <strong>it</strong>self


OOP Example<br />

• Class constructor<br />

• Must build an object!<br />

• Method w<strong>it</strong>h same name<br />

as class<br />

• W/o constructor,<br />

properties get default<br />

values<br />

• Here, we ensure A and B<br />

are properly handled


• Overloading<br />

OOP<br />

• method(myObject) calls class method instead<br />

of general function method if <strong>it</strong> exists<br />

• Define how basic functions handle class (plot, ( plot,<br />

…)<br />

display, plus, get, set, subsref…)<br />

subsref<br />

• Simpler function names!


OOP Example<br />

• Overloading<br />

• Changing built-in<br />

functions to handle<br />

our class


OOP Example


OOP Example<br />

• Inher<strong>it</strong>ance <strong>–</strong> building upon existing classes<br />

• Gets all fields and methods from parent<br />

• Now we can overload parent functions!<br />

Passes arguments to<br />

parent constructor<br />

Could add props or change defaults if desired<br />

Calls eval in parent class, passing in<br />

the LogPoly. object lp<br />

Declares that L.P. inher<strong>it</strong>s from P.


Inher<strong>it</strong>ance<br />

• Inher<strong>it</strong>ance<br />

OOP<br />

• Wr<strong>it</strong>e hierarchy of classes<br />

• Define methods/properties in root, all subclasses<br />

get them<br />

• Modify subclasses as needed<br />

• All subclasses now benef<strong>it</strong> from same set of<br />

functions<br />

• Much better than wr<strong>it</strong>ing similar functions for<br />

many types of data!!!


• Inher<strong>it</strong>ance <strong>–</strong> Example<br />

PsychExp class<br />

OOP<br />

• PsychExp<br />

• Methods to set up computer for experiment, load subject data,<br />

create datafile, calibrate equipment<br />

• VisualExp inher<strong>it</strong>s PsychExp<br />

• Methods to create visual stimuli, saves slightly different subject<br />

data, convert pixels to degrees of visual angle...<br />

• Now create a VisualExp object and you have all the general<br />

experiment methods and the specific ones<br />

• AudioExp inher<strong>it</strong>s PsychExp<br />

• Now, modifying PsychExp will update all classes<br />

which inher<strong>it</strong> <strong>it</strong>!


• The Big Advantages<br />

• Encapsulation<br />

• Overloading<br />

• Inher<strong>it</strong>ance<br />

OOP<br />

• Aesthetics/Deployment/Collaboration


• Class Tricks<br />

OOP<br />

• Object fields can contain child objects!<br />

• At command prompt, we get the following:<br />

lists all the defined methods<br />

• >> methods myClass lists all the defined methods<br />

• >> help @myClass lists defined methods and the first<br />

line of their documentation<br />

• >> ed<strong>it</strong> myClass/myMethod<br />

Pass by value: obj = method(obj);<br />

• Pass by value:


• Class Tricks<br />

• struct(myObject)<br />

OOP<br />

struct(myObject) returns the structure inside the<br />

object, allows you to view data w<strong>it</strong>hout encapsulation<br />

(great for debugging)<br />

• To avoid encapsulation<br />

• function p = get(myObj, property)<br />

p = myObj.(property);<br />

• Similar w<strong>it</strong>h set<br />

• Also can overload subsref to customize myObject.x,<br />

myObject(x), myObject{x} usage


• Wrap up OOP<br />

Monday<br />

• Dynamic Programming<br />

• An introduction to ducks

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!