This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

116.001 SICPObject Oriented Programming• Data Abstraction using Procedures with State• Message-Passing • Object Oriented Modeling• Class diagrams• Instance diagrams• Example: spacewar simulation2The role of abstractions• Procedural abstractions• Data abstractions•Questions:•How easy is it to break system into abstraction modules?•How easy is it to extend the system?•Adding new data types?•Adding new methods?Goal: treat complex things as primitives, and hide details3One View of Data• Data structures• Some complex structure constructed from cons cells– point, line, 2dshape, 3dshape• Explicit tags to keep track of data types– (define (make-point x y) (list 'point x y))• Implement a data abstraction as set of procedures that operate on the data•"Generic" operations by looking at types: (define (scale x factor)(cond ((point? x) (point-scale x factor))((line? x) (line-scale x factor))((2dshape? x)(2dshape-scale x factor))((3dshape? x)(3dshape-scale x factor))(else (error "unknown type"))))4Generic Operations3dshape-scale2dshape-scaleline-scalepoint-scalescale 3-dShape2-dShapeLinePoint5Generic Operations3dshape-trans2dshape-transline-transpoint-transtranslate3dshape-scale2dshape-scaleline-scalepoint-scalescale 3-dShape2-dShapeLinePoint6Generic Operations3dshape-color2dshape-colorline-colorpoint-colorcolor3dshape-trans2dshape-transline-transpoint-transtranslate3dshape-scale2dshape-scaleline-scalepoint-scalescale 3-dShape2-dShapeLinePoint27Generic Operations• Adding new methods• Just create generic operations3dshape-color2dshape-colorline-colorpoint-colorcolor3dshape-trans2dshape-transline-transpoint-transtranslate3dshape-scale2dshape-scaleline-scalepoint-scalescale 3-dShape2-dShapeLinePoint8Generic Operations• Adding new methods• Just create generic operations…………new-op3dshape-color2dshape-colorline-colorpoint-colorcolor3dshape-trans2dshape-transline-transpoint-transtranslate3dshape-scale2dshape-scaleline-scalepoint-scalescale 3-dShape2-dShapeLinePoint9Generic Operations• Adding new methods• Just create generic operations• Adding new data types• Must change every generic operation• Must keep names distinct…………new-op3dshape-color2dshape-colorline-colorpoint-colorcolor3dshape-trans2dshape-transline-transpoint-transtranslate3dshape-scale2dshape-scaleline-scalepoint-scalescale 3-dShape2-dShapeLinePointcurvetrans-ccolor-c…scale-c10Views of The WorldData object…………new-op3dshape-color2dshape-colorline-colorpoint-colorcolor3dshape-trans2dshape-transline-transpoint-transtranslate3dshape-scale2dshape-scaleline-scalepoint-scalescale 3-dShape2-dShapeLinePointcurvetrans-ccolor-c…scale-cGeneric operation11Thinking About Data Objects• A data type, but….• it has operations associated with it• we want both the generic concept (a line), anda specific instance (line17)• the specific instance can have private data associated with it (e.g., its endpoints)• AKA: object oriented programming12Scheme OOP: Procedures with State• A procedure has• parameters and body as specified by λ expression• environment (which can hold name-value bindings!)•Can use procedure to encapsulate (and hide) data, and provide controlled access to that data•Procedure application creates private environment•Need access to that environment•constructor, accessors, mutators, predicates, operations•mutation: changes in the private state of the procedure313Programming Styles –Procedural vs. Object-Oriented• Procedural programming:• Organize system around procedures that operate on data(do-something <data> <arg> ...)(do-another-thing <data>)•Object-based programming:•Organize system around objects that receive messages(<object> 'do-something <arg>)(<object> 'do-another-thing)•An object encapsulates data and operations14Object-Oriented Programming Terminology• Class: • specifies the common behavior of entities• in scheme, a <type> procedure• Instance:• A particular object or entity of a given class• in scheme, an instance is a message-handling procedure made by a create-<type> procedure15Using classes and instances to design a system• Suppose we want to build a spacewar game• I can start by thinking about what kinds of objects do I want (what classes, their state information, and their interfaces)• ships• planets• other objects• I can then extend to thinking about what particular instances of objects are useful • Millenium Falcon• Enterprise•Earth16SPACEWAR: the original video gamefirst realized on the MIT PDP-1 in 1962PDP-1 – 100KHz, 4K Ram, $100,00017A Space-Ship Object(define (make-ship position velocity num-torps)(define (move)(set! position (add-vect position ...)))(define (fire-torp)(cond ((> num-torps 0) ...)(else 'FAIL)))(lambda (msg)(cond ((eq? msg 'POSITION) position)((eq? msg 'VELOCITY) velocity)((eq? msg 'MOVE) (move))((eq? msg 'ATTACK) (fire-torp))(else (error "ship can't" msg)))))18Space-Ship ClassSHIPposition:velocity:num-torps:POSITIONVELOCITYMOVEATTACKclassprivate statepublicmessages419Example – Instance Diagram(define enterprise(make-ship (make-vect 10 10) (make-vect 5 0) 3))(define war-bird(make-ship (make-vect -10 10) (make-vect 10 0) 10))SHIPpos: (vec 10 10)vel: (vec 5 0)num-torps: 3SHIPpos: (vec 10 10)vel: (vec 5 0)num-torps: 3enterprisewar-birdSHIPposition:velocity:num-torps:POSITIONVELOCITYMOVEATTACKinstance of shipinstance of ship20position: (vec 10 10)velocity: (vec 5 0)num-torps: 311Example – Environment Diagram(define enterprise(make-ship (make-vect 10 10) (make-vect 5 0) 3))(enterprise ‘MOVE) ==> DONE(enterprise ‘POSITION) ==> ? GEpar: msgbody: (cond …)enterprise:move:fire-torp:par:body: (set! position …)(vec 15 10)2From internal definitions(vec 15 10)(define (make-ship position velocity num-torps)(define (move)(set! position (add-vect position ...)))(define (fire-torp)(cond ((> num-torps 0) ...)(else 'FAIL)))(lambda (msg)(cond ((eq? msg 'POSITION) position)((eq? msg 'VELOCITY) velocity)((eq? msg 'MOVE) (move))((eq? msg 'ATTACK) (fire-torp))(else (error "ship can't" msg)))))21Filling out our World-- how do we think about programming in this space”?• Add a PLANET class to our world•Add predicate messages so we can check type of objects• Add display handler to our system• Draws objects on a screen• Can be implemented as a procedure (e.g. draw) -- not everything has to be an object!•Add 'DISPLAY message to classes so


View Full Document

MIT 6 001 - Lecture Notes

Documents in this Course
Quiz 1

Quiz 1

6 pages

Databases

Databases

12 pages

rec20

rec20

2 pages

Quiz II

Quiz II

15 pages

Streams

Streams

5 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?