6.001 Recitation # 13 – March
21, 2003
RI: Konrad
Tollmar
•Generic OOP
•Parsing object messages
Startade in Norway 1962
(Norsk Regnesentral) to develop a language for simulations. The system functions are
created by mirror the real object that are part of a process.
OO Concepts that we will
talk about:
1. A Employee Class
Create a OO model of people employed in a company
(employers). In this company have different employers different roles such as;
sales personnel, marketing personnel, and administrative personnel. Each role
has some unique properties but they also share some properties, e.g., name of
the person. Create appropriate classes and use inheritance and aggregates to
make the right associations between these classes.
(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 (make-emp name)
(lambda
(msg)
(case
msg
((getName)
(lambda (self) name)))))
(define eecs-emp (make-emp 'konrad))
(ask eecs-emp 'getName)
Make a Scheme implementation of the classes Employee
(define eecs-emp (make-emp 'konrad))
(ask eecs-emp 'getName)
;Value: konrad
(ask eecs-emp 'setRole '6001-recitator)
;Value: #f
(ask eecs-emp 'getRole)
konrad is our:
6001-recitator
;Value: 6001-recitator
(define (make-emp name)
(let ((role
nil))
(lambda
(msg)
(case
msg
((getName)
(lambda (self) name))
((setRole)
(lambda (self newrole)
(set! role newrole)))
((getRole)
(lambda (self)
(display (ask self 'getName))
(display " is our: ")
(display role)
role))))))
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
(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)))
(else
(no-method))))
4. Object Oriented Variables
(define
(create-var value)
(let ((super (create-stack)))
(ask super ’push value)
(lambda (message)
(case message
((SET!)
(lambda (self new)
(ask super ’push new)))
((GET)
(lambda (self)
(ask super ’peek)))
((UNDO!)
(lambda (self)
(ask super ’pop))
(else
(no-method))))))