Transcript 11.2 AdtOop

CLOS: Common Lisp Object System
•
•
•
•
•
•
•
Basic principles of CLOS
Classes, Instances and Slots
Inheritance
Generic functions
Methods
Multi-methods
Comparison CLOS and Java
1
CLOS: Classes, Instances and Slots
• Classes
– Common Lisp types
• Instances of a class
– Share structure, behavior, and type
• Slots
–
–
–
–
–
Determine the structure of a class
Have a name and a value
Can be read and written by slot accessors
Local slots maintain a separate value for each instance
Shared slots maintain a common (class-wide) value
2
CLOS: Superclasses
• Allow classes to be built from other classes
• New class inherits structure and behavior from
superclasses
• Classes can inherit structure and behavior from multiple
superlasses.
3
CLOS Elements: Generic functions
• Syntactically identical to normal CL functions
• Semantic difference 1
– Normal functions specify interface and implementation
– Generic functions specify only interface
• Implementation is distributed among a set of methods
• Semantic difference 2
– Implementation of a normal function doesn't vary from call to call
– Implementation of a generic function varies depending upon the
class of its arguments
• Behavior of a generic function involves both
– Selection of methods, and
– Composition of methods
4
CLOS Elements: Methods
• The underlying implementation of generic functions
– Methods are similar to ordinary Lisp functions, but
– They are not called directly
• Method “role” determines its part in generic function
– Primary methods perform bulk of work
• Only one primary method is called each time a generic function is
called
• It provides the return value of the generic function
– Before methods called before the primary
– After methods called after the primary
– Around methods “sandwich” the primary
5
CLOS Elements: Inheritance
• Inheritance is the sharing of characteristics and behavior
among a set of classes
• Both slots and methods can be shared among classes
• Multiple inheritance creates problem of differently
defined methods or slots with the same name in different
ancestors
• CLOS computes a class precedence list to determine
which method to inherit
– A class always has precedence over its superclasses
– The order in which classes are listed in a class definition sets
determines their precedence
6
Example class hierarchy
window
window
with-label
window
with-border
primary method: refresh
Code to
Clear Window
generic
function
refresh
window
after method: refresh
Code to
draw border
windowwith-border
after method: refresh
Code to
draw label
windowwith-label
7
Example refresh invocation
Instance of
window-with-label
argument
refresh
implementation
side effects
value
Implementation:
primary method: refresh
Code to
Clear Window
window
after method: refresh
Code to
draw label
windowwith-label
8
Method combination
• The process of determining the implementation
associated with a generic function for a given set
of arguments is called generic dispatch
– Find all applicable methods
– Sort them by order of precedence
– Call one or more of them
• Method ordering
– All before methods in most-specific-first order
– The most specific primary method
– All after methods in most-specific-last order
9
Example: window-with-border instance
before method for window-with-border
before method for window
most specific primary method
after method for window
after method for window-with-border
10
Slot access and method combination
• Slot accessors are simply primary methods
– (defclass triangle (shape)
((side-a :accessor side-a)
(side-b :accessor side-b)
(side-c :accessor side-c)
(area :reader area)))
• We can update the area slot automatically whenever a
side changes value by defining an :after method
– (defmethod (setf side-a) :after
(new-length (tri triangle))
(setf (area tri) <new area>))
11
Multi-methods
12
• Methods can be written to specialize on multiple parameters.
• Multi-methods allow methods to “belong” to more than one class
simultaneously.
basicproduct
Life
Adventure
basicOS
Unix
(defmethod install((sw basic-product)
(os basic-OS))
(restore-product sw os)
(compile-product sw os)
(configure-site sw os)
(verify-product sw os))
Windows
Meta-object protocol (MOP)
• Used to define default model of OO above.
• Allows implementation of alternative paradigms
• Specifies:
– What a subclass inherits from its superclasses
– How a subclass determines the precedence of superclasses
– How generic functions dispatch
• Classes are themselves instances of a meta-class
• Meta-classes specify the structure and behavior of
classes
• Meta-classes inherit structure and behavior from their
super meta-classes
13
CLOS vs. Java
• Java does not support generic functions
– Java methods analogous to CLOS methods
– Java does not explicitly support member function
composition
• Java does not support multi-methods
– Methods owned by a single class
• Java does not provide a meta-object protocol
– Inheritance and dispatch procedure hardwired.
• Java provides access control (public, private..)
14