110 likes | 121 Views
Explore representations of objects in software through entity-relationship diagrams, object models, and Scheme programming. Learn to map real-world concepts into classes, associations, and inheritance. Understand how representations are linked with practical examples, constraints, and implementation details.
E N D
6001structure & interpretation of computer programsrecitation 14/ november 12, 1997
topics • three representations • entity relationship diagram • object model • Scheme representation
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
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
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
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”
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
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
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)
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)
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!