1 / 11

6001 structure & interpretation of computer programs recitation 14/ november 12, 1997

6001 structure & interpretation of computer programs recitation 14/ november 12, 1997. topics. three representations entity relationship diagram object model Scheme representation. levels of abstraction. last time we looked at how to

druiz
Télécharger la présentation

6001 structure & interpretation of computer programs recitation 14/ november 12, 1997

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 6001structure & interpretation of computer programsrecitation 14/ november 12, 1997

  2. topics • three representations • entity relationship diagram • object model • Scheme representation

  3. levels of abstraction • last time we looked at how to • model the real world with an entity-relationship diagram (classes, is-a, associations) • design an object model (classes, inheritance, instance vars, methods) • and we briefly mentioned • a way to simulate objects in Scheme • today • we’ll go more deeply into how these 3 representations are related • using examples from problem set 7

  4. entity-relationship diagram • classes in an adventure game • named, mobile, place, person, thing, transporter, troll, rover, rock • associations • owns, at, adjoins, transports_to • exercise • arrange the classes in an is-a hierarchy • mark the associations between appropriate classes • note that a class can be associated with itself • what the ER diagram does not mention • how associations are implemented: who has pointers to whom • how state and methods are allocated to classes

  5. object model • now allocate instance variables • name • location • things • neighbours • possessions • owner • now invent some methods • note that a subclass need not add instance vars (eg troll) • subclass can override methods

  6. consistency constraints • how they arise • when an association is represented by multiple instance vars • the instance vars of different objects are related • example • the owns association • person has possessions, thing has owner • how they are maintained • cannot be maintained all the time! • when one object changes, it calls methods on the related objects • abstract constraints • not all constraints arise from implementation • example: • destination assocation connects Transporter to Place • might want to say that the destination cannot be a transporter • or that there are no “transporter loops”

  7. scheme representation of objects • how objects are represented in the problem set • an object O is represented by a chain of Scheme closures,one for each class in the path upwards in the inheritance hierarchy • each subclass closure contains a pointer to its superclass closure • each closure has a self pointer that always points to the object O • confusing terminology • the superclass closure is said to be a “part” of the subclass closure • but not a part in the modelling sense: Vehicle is a part of Car,Wheel is not a part of Car! • how parts differ from objects • their self pointers don’t point to themselves • each is accessible only to the one closure that contains it • example • see figure 1 of PS7

  8. closures revisited • a closure is • a procedure and an environment • the environment is accessible only to the procedure • so closure represents encapsulated state • example: bank account with one operation (withdraw) • (define (make-withdraw bal) (lambda (i) (set! bal (- bal i)) bal)) • (define w1 (make-withdraw 100)) • (define w2 (make-withdraw 100)) • (w1 10) ==> 90 • (w1 10) ==> 80 • (w2 10) ==> 90 • draw environment diagram

  9. dispatch • many bank accounts, many operations • procedure that makes accounts: • (define (make-account bal)(define (withdraw amt) (set! bal (- bal amt)) bal)(define (deposit amt) (set! bal (+ bal amt)) bal)(define (dispatch m) (cond ((eq? m ‘withdraw) withdraw) ((eq? m ‘deposit) deposit)))dispatch) • example of use • (define a1 (make-account 50)) • ((a1 ‘deposit) 40) • ((a1 ‘withdraw) 60)

  10. putting local procedures in a new frame • in the problem set object representation • the method procedures are bound in a fresh frame: • (define (make-account bal)(let ((withdraw (lambda (amt) …))) (let ((dispatch (lambda (m) …))) dispatch)

  11. puzzle for student presentation • build a “shell” with history that works like this: • (define m (mk-shell)) • (m ‘execute p) ; executes the procedure p and sets p to be the current proc • (m ‘back) ; sets the current proc back one • (m ‘forward) ; sets the current proc forward one • (m ‘run) ; executes the current proc • hint • use the circular buffer we designed last time!

More Related