MASSACHVSETTS INSTITVTE
OF TECHNOLOGY
Department of Electrical
Engineering and Computer Science
6.001 -- Structure and Interpretation of Computer Programs
March 20, 1997
Lecture Notes
Conventions for Object-Oriented Programming
- OO Programming is about conventions for handling local state:
- What is in the state?
- How access is granted to the state?
- How is the state created, initialized, and modified?
- How code for handling the state can be shared for reuse?
Terminology for Object-Oriented Programming
- Terminology: Access to state is via
objects. An object can receive
messages, and has access to various
methods for responding to them.
- Terminology: Objects are created by
classes, which specify what state is to be created
when a new object is created, what messages the object will process,
which methods it will use to process those messages, and what to do if
the object receives a message for which it has no method.
- The classes are related to each other through
inheritance or delegation. We'll
explore delegation (it's a bit more powerful) but Java uses
inheritance.
- In Scheme, objects and classes are both procedures. The local
state is stored in the frames that are created when the procedures are
called. This is also true of Java.
Basic Operations for Objects
(define (get-method-from-object message object)
(object message))
(define get-method ; Simplification
get-method-from-object)
(define no-method
(let ((tag (list 'NO-METHOD)))
(lambda () tag)))
(define (method? x)
(cond ((procedure? x) #T)
((eq? x (no-method)) #F)
(else
(error
"Object returned this non-message:" x))))
Pairs With Conventions
(define (make-pair L R)
(lambda (msg)
(case msg
((LEFT) (lambda () L)
((RIGHT) (lambda () R)
((NEW-LEFT!) (lambda (new) (set! L new)))
((NEW-RIGHT!) (lambda (new) (set! R new)))
(else (no-method)))))))
Named Objects
(define (make-named-object name)
;; All objects deletegate to this, so we are
;; guaranteed that any object can respond to the
;; messages NAME, INSTALL, and SAY.
(lambda (message)
(case message
((NAMED-OBJECT?) (lambda (self) #T))
((NAME) (lambda (self) name))
((SAY)
(lambda (self list-of-stuff)
(if (not (null? list-of-stuff))
(display-message list-of-stuff))
'NUF-SAID))
((INSTALL) (lambda (self) 'INSTALLED))
(else (no-method))))))
Delegation
- Idea: capture common information (state and methods) for reuse
rather than reinvention.
- Idea: when you want to have some behavior, create an object
that has that behavior as part of your local state, and then delegate
work to it when needed.
- Problem: you have to know who you are, so that operations
that work differently with you will have your special stamp on them.
"If I delegate a task to you, I expect you to do it the way I would
have done it."
- Solution: Every method has a first argument that is the
"self" on whose behalf the operation is performed.
Places delegate to Named-Object
(define (make-place name)
(let ((neighbor-map '()) ; Alist, direction -> object
(things '())
(named-obj (make-named-object name)))
(lambda (message)
(case message
((PLACE?) (lambda (self) #T))
...
(else (get-method message named-obj)))))))