170 likes | 383 Views
CLOS: Common Lisp Object System. Basic principles of CLOS Classes, Instances and Slots Inheritance Generic functions Methods Multi-methods Comparison CLOS and Java. CLOS: Classes, Instances and Slots. Classes Common Lisp types Instances of a class Share structure, behavior, and type
E N D
CLOS: Common Lisp Object System • Basic principles of CLOS • Classes, Instances and Slots • Inheritance • Generic functions • Methods • Multi-methods • Comparison CLOS and Java
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
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.
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
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
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
Example class hierarchy window window with-label window with-border primary method: refresh Code to Clear Window window generic function after method: refresh Code to draw border window- with-border refresh after method: refresh Code to draw label window- with-label
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 window- with-label
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
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
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>))
Multi-methods • Methods can be written to specialize on multiple parameters. • Multi-methods allow methods to “belong” to more than one class simultaneously. basic- product basic- OS Life Adventure Unix Windows (defmethod install((sw basic-product) (os basic-OS)) (restore-product sw os) (compile-product sw os) (configure-site sw os) (verify-product sw os))
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
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..)