DOC PREVIEW
MIT 6 001 - Object Oriented Programming

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 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 32 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 32 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 32 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 32 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 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

6.001 SICP Object Oriented ProgrammingThe role of abstractionsOne View of DataDispatch on TypeAn Alternative View of Data: Procedures with StateExample: Pair as a Procedure with StateExample: What is our "pair" object?Pair Mutation as Change in StateExample: Mutating a pair objectMessage Passing Style - RefinementsVariable number of argumentsSlide 12Programming Styles – Procedural vs. Object-OrientedObject-Oriented Programming TerminologyClass Diagram Instance DiagramUsing classes and instances to design a systemA Space-Ship ObjectSpace-Ship ClassExample – Instance DiagramExample – Environment DiagramSome Extensions to our WorldSlide 22Planet ImplementationFurther Extensions to our WorldClass DiagramCoordinating with a clockThe Universe and TimeControlling the clockImplementations for our Extended WorldTorpedo ImplementationRunning the SimulationSummary6.001 SICP 1/306.001 SICPObject Oriented Programming•Data Abstraction using Procedures with State•Message-Passing •Object Oriented Modeling•Class diagrams•Instance diagrams•Example: space wars simulation6.001 SICP 2/30The 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 details6.001 SICP 3/30One View of Data•Tagged data:•Some complex structure constructed from cons cells•Explicit tags to keep track of data types•Implement a data abstraction as set of procedures that operate on the data •"Generic" operations by looking at types: (define (scale x factor) (cond ((number? x) (* x factor)) ((line? x) (line-scale x factor)) ((shape? x) (shape-scale x factor)) (else (error "unknown type"))))6.001 SICP 4/30Dispatch on Type•Adding new data types:•Must change every generic operation•Must keep names distinct•Adding new methods:•Just create generic operationsData type 1 Data type 2 Data type 3 Data type 4Operation 1 Some proc Some proc Some proc Some procOperation 2 Some proc Some proc Some proc Some procOperation 3 Some proc Some proc Some proc Some procOperation 4 Some proc Some proc Some proc Some procGeneric operationGeneric data object?6.001 SICP 5/30An Alternative View of Data: 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 procedure6.001 SICP 6/30Example: Pair as a Procedure with State(define (cons x y) (lambda (msg) (cond ((eq? msg ‘CAR) x) ((eq? msg ‘CDR) y) ((eq? msg ‘PAIR?) #t) (else (error "pair cannot" msg)))))(define (car p) (p ‘CAR))(define (cdr p) (p ‘CDR))(define (pair? p) (and (procedure? p) (p ‘PAIR?)))6.001 SICP 7/30Example: What is our "pair" object?(define foo (cons 1 2))GEp: x ybody: ( (msg) (cond ..))cons:1p: msgbody: (cond ...)E1foo:x: 1y: 21(car foo) | GE=> (foo 'CAR) | E2=>2(cond ...) | E3=> x | E3=> 1msg: CARE3323(car foo) becomes (foo 'CAR)(define (cons x y) (lambda (msg) (cond ((eq? msg ‘CAR) x) ((eq? msg ‘CDR) y) ((eq? msg ‘PAIR?) #t) (else (error …)))))(define (car p) (p ‘CAR))6.001 SICP 8/30Pair Mutation as Change in State(define (cons x y) (lambda (msg) (cond ((eq? msg ‘CAR) x) ((eq? msg ‘CDR) y) ((eq? msg ‘PAIR?) #t) ((eq? msg ‘SET-CAR!) (lambda (new-car) (set! x new-car))) ((eq? msg ‘SET-CDR!) (lambda (new-cdr) (set! y new-cdr))) (else (error "pair cannot" msg)))))(define (set-car! p new-car) ((p ‘SET-CAR!) new-car))(define (set-cdr! p new-cdr) ((p ‘SET-CDR!) new-cdr))6.001 SICP 9/30Example: Mutating a pair object(define bar (cons 3 4))GE(cond ...) | E6=> ( (new-car) (set! x new-car)) | E6msg: SET-CAR!E63p: new-carbody: (set! x new-car)4(set! x new-car) | E7new-car: 0E75(set-car! bar 0) | GE=> ((bar 'SET-CAR!) 0) | E52(set-car! bar 0)6changes x value to 0 in E401p: msgbody: (cond ...)E4bar:x: 3y: 416.001 SICP 10/30Message Passing Style - Refinements•lexical scoping for private state and private procedures(define (cons x y) (define (change-car new-car) (set! x new-car)) (define (change-cdr new-cdr) (set! y new-cdr)) (lambda (msg . args) (cond ((eq? msg ‘CAR) x) ((eq? msg ‘CDR) y) ((eq? msg ‘PAIR?) #t) ((eq? msg ‘SET-CAR!) (change-car (first args))) ((eq? msg ‘SET-CDR!) (change-cdr (first args))) (else (error "pair cannot" msg)))))(define (car p) (p 'CAR))(define (set-car! p val) (p 'SET-CAR! val))6.001 SICP 11/30Variable number of argumentsA scheme mechanism to be aware of:•Desire: (add 1 2) (add 1 2 3 4)•How do this? (define (add x y . rest) ...) (add 1 2) => x bound to 1 y bound to 2 rest bound to '() (add 1) => error; requires 2 or more args (add 1 2 3) => rest bound to (3) (add 1 2 3 4 5) => rest bound to (3 4 5)6.001 SICP 12/30Message Passing Style - Refinements•lexical scoping for private state and private procedures(define (cons x y) (define (change-car new-car) (set! x new-car)) (define (change-cdr new-cdr) (set! y new-cdr)) (lambda (msg . args) (cond ((eq? msg ‘CAR) x) ((eq? msg ‘CDR) y) ((eq? msg ‘PAIR?) #t) ((eq? msg ‘SET-CAR!) (change-car (first args))) ((eq? msg ‘SET-CDR!) (change-cdr (first args))) (else (error "pair cannot" msg)))))(define (car p) (p 'CAR))(define (set-car! p val) (p 'SET-CAR! val))6.001 SICP 13/30Programming 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 operations6.001 SICP 14/30Object-Oriented Programming Terminology•Class: •specifies the common behavior of entities•in scheme, a "maker" procedure•Instance:•A particular object or entity of a given


View Full Document

MIT 6 001 - Object Oriented Programming

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 Object Oriented Programming
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 Object Oriented Programming 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 Object Oriented Programming 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?