6.001 Recitation # 14 – April
2, 2003
RI: Konrad
Tollmar
• Class inheritance
• Delegation
Analysis - Models without any language constraints
Design - Models restricted to a certain computational model
Implementation - Code
Classes - Attributes and methods
Inheritance - Relations between classes
Aggregates - Composed objects
Associations - Connection between objects
(define (get-method message object)
(object message))
(define method? Procedure?)
(define (ask object message . args)
(let ((method (get-method
message object )))
(if (method? method)
(apply method object args) ;object becomes self
(error "No method for message" message))))
(define
(delegate to from message . args)
(let ((method (get-method message to)))
(if (method? method)
(apply method from args) ;from becomes self
(error "No method" message))))
2. An OO System
A/ Make a Scheme implementation of the classes Employee and Role.
B/ Extend the class Role with the class SalePerson.
3. Object Oriented Stacks
Using this object-oriented style, write the function create-stack that will create a stack object. Recall that stacks are data structures that include the operations push, pop, peek, and clear. Objects get pushed on and popped off the stack in a last-in-first-out manner. Complete the function create-stack.
(define s (create-stack)
(ask s ’push 5)
(ask s ’push 3)
(ask s ’pop) ==>
3
(ask s ’push 1)
(ask s ’pop) ==>
1
(ask s ’pop) ==>
5
4. Object Oriented Variables
Next, let's write an abstraction for variables in the object oriented style. We do this so that in addition to the
get (lookup) and set! operations that we have in scheme, we also want to implement an undo! method that un-does the last set!ing of the variable. We want to store an arbitrary number of undos.
Hint: How can we use stacks to help us with this
undo!?
(define a (create-var 1)
(ask a ’get) ==>
1
(ask a ’set 2)
(ask a ’get) ==>
2
(ask a ’undo)
(ask a ’get) ==> 1
undo! the results to set, let's add a redo! feature,
such that successive calls to undo! and be taken back using a call to redo!.
(define a (create-var 1)
(ask a ’set! 2)
(ask a ’get) ==>
2
(ask a ’undo)
(ask a ’get) ==>
1
(ask a ’redo)
(ask a ’get) ==>
2
Project 3
(define (make-root-object)
(lambda (message)
(no-method)))
(define (make-named-object name)
(let ((root-part (make-root-object)))
(lambda (message)
(case message
((NAMED-OBJECT?)
(lambda (self) #T))
((NAME)
(lambda (self) name))
((INSTALL)
(lambda (self) 'INSTALLED))
((DESTROY)
(lambda (self) 'DESTROYED))
(else
(get-method message root-part))))))
(define foo (make-named-object
'george))
(define (make-thing name
location)
(let ((named-object-part (make-named-object name)))
(lambda (message)
(case message
((THING?)
(lambda (self) #T))
((LOCATION)
(lambda (self) location))
((INSTALL)
(lambda (self) ; Install: synchronize thing
and place
(ask (ask self 'LOCATION) 'ADD-THING self)
(delegate named-object-part self
'INSTALL)))
((DESTROY)
(lambda (self) ; Destroy: remove from place
(ask (ask self 'LOCATION) 'DEL-THING self)
(delegate named-object-part self
'DESTROY)))
((EMIT)
(lambda (self text) ; Output some text
(ask screen 'TELL-ROOM (ask self 'LOCATION)
(append
(list "At" (ask (ask self 'LOCATION) 'NAME))
text))))
(else
(get-method message named-object-part))))))
(define (is-a object type-pred)
(if (not (procedure? object))
#f
(let ((method (get-method type-pred object)))
(if
(method? method)
(ask object type-pred)
#F))))
(define my-book (make-thing
'great-gatsby 'dark-room))
(is-a my-book 'THING?)
(is-a my-book 'BOOK?)