6.001 Recitation # – April 2,
2003
RI: Konrad
Tollmar
•Class inheritance
•Delegation
2. A OO System
(define
(make-role department)
(lambda (msg)
(case msg
((Role?) (lambda(self) #t))
((printDepartment)
(lambda (self)
(display department)
(display " "))))))
(define
(make-emp name)
(let ((roles '()))
(lambda (msg)
(case msg
((Emp?) (lambda (self) #t))
((getName) (lambda (self) name))
((addRole) (lambda (self role)
(set! roles (cons role roles)) 'yes ))
((printRoles) (lambda (self)
(display (ask
self 'getName))
(display "
")
(map (lambda
(x)
(ask x
'printDepartment)) roles)))))))
(define
6001-recit (make-emp 'konrad))
(ask
6001-recit 'addRole (make-role 'eecs))
(ask
6001-recit 'addRole (make-role 'ai))
(ask
6001-recit 'printRoles)
B/ Inheritance
(define (make-saleperson solditems department)
(let ((role
(make-role department)))
(lambda
(msg)
(case msg
((SalePerson?) (lambda (self) #t))
((getBonus) (lambda (self) solditems))
(else (get-method msg role))))))
(define (make-emp name)
(let
((roles '()))
(lambda
(msg)
(case
msg
((Emp?)
(lambda (self) #t))
((getName)
(lambda (self) name))
((addRole)
(lambda (self role)
(set! roles (cons role roles)) 'yes ))
((getSalary)
(lambda (self)
(display (ask self 'getName))
(display " ")
(ask (ask self 'getMainRolemap) 'getSalary))
((getMainRole)
(lambda (self) (car roles))
((printRoles)
(lambda (self)
(display
(ask self 'getName))
(display
" ")
(map
(lambda (x) (ask x 'printDepartment)) roles)))
(else
(error "what?" msg))))))
(define
(make-role department salary)
(lambda
(msg)
(case msg
((Role?) (lambda (self) #t))
((printDepartment) (lambda (self)
(display
department) (display " ")))
((getSalary) (lambda (self)
(+ salary (ask self 'getBonus)))))))
(define (make-saleperson department salary)
(let (
(role (make-role department salary))
(solditems 0))
(lambda
(msg)
(case
msg
((getBonus)
(lambda (self) solditems))
((addSoldItems)
(lambda (self)
(set! solditems (+ 1 solditems))))
((getSalary)
(lambda (self)
(delegate role self 'getSalary)))
(else
(get-method msg role))))))
(define (make-supportperson department)
(let (
(role (make-role department 50))
(years 0))
(lambda
(msg)
(case
msg
((getBonus)
(lambda (self) (* years 100)))
((addYears)
(lambda (self)
(set! years (+ 1 years))))
((getSalary)
(lambda (self)
(delegate role self 'getSalary)))
(else
(get-method msg role))))))
(define 6001-rec (make-emp 'konrad))
(define 6001-role (make-supportperson 'eecs))
(ask 6001-rec 'addRole 6001-role)
(ask (ask 6001-rec 'getMainRole) 'addYears)
(ask 6001-rec 'getSalary)
(define ai (make-emp 'et))
(define ai-role (make-saleperson 'ai 100))
(ask ai 'addRole ai-role)
(ask (ask ai 'getMainRole) 'addSoldItems)
(ask (ask ai 'getMainRole) 'addSoldItems)
(ask ai 'getSalary)
2. Object Oriented Stacks
(define
(create-stack)
(let ((value '()))
(lambda (message)
(case message
((PEEK)
(lambda (self)
(car value)))
((PUSH)
(lambda (self new)
(set! value (cons new value))))
((CLEAR)
(lambda (self)
(set! Value '())))
((POP)
(lambda (self)
(let ((return (ask self 'PEEK)))
(set! value (cdr value)) return)))))))
3. Object Oriented Variables
(define
(create-var value)
(let ((undo (create-stack)))
(ask undo ’push value)
(lambda (message)
(case message
((SET!)
(lambda (self new)
(ask undo ’push new)))
((GET)
(lambda (self)
(ask undo ’peek)))
((UNDO!)
(lambda (self)
(ask undo ’pop)) )))))
4. Object Oriented
Variables with Redo!
(define
(create-var value)
(let ((undo (create-stack))
((redo (create-stack)))
(ask undo ’push value)
(lambda (message)
(case message
((SET!)
(lambda (self new)
(ask redo ’clear)))
(ask undo ’push
new)))
((GET)
(lambda (self)
(ask undo ’peek)))
((UNDO!)
(lambda (self)
(ask redo ’push (ask undo ’pop))
((REDO!)
(lambda (self)
(ask undo ’push (ask redo ’pop)))))))