Previous Table of Contents Next


5.2. An Introduction to CLOS

A computer program uses a model to guide the implementation. When implementing the system, it’s often convenient to think about the system as objects: entities that have characteristics. Object-oriented programming allows the program to represent as similar in source code entities that differ in details, but are convenient to think about as similar. These entities are known as objects. Objects with the same characteristics are bundled as generic types known as classes. Objects might represent managers, employees, and departments in a business application or might represent events, locks, and windows in a window system.

CLOS makes it easy to model a system using objects. A window system may have many components that conceptually handle the same operations: Window panes and scrollbars both want to support operations such as mouse-in, mouse-out, and mouse-click. The underlying implementation of what a mouse-click does is quite different in a window pane and in a scrollbar, but conceptually, it is a lot simpler to treat them as being the same. CLOS has a rich, flexible, and complete object model.

Like other object systems, CLOS supports separation of implementation from the interface. Objects are known only by their interface, or the set of operations that can be performed on them. Other components of the application use these operations to create and interact with these objects and do not have knowledge of the actual implementation of each underlying object.

The separation of interface from implementation is one of the key benefits of object programming: Objects may be modified, extended, or implemented differently without the rest of the program, which depends only on the interface, needing to be changed. New objects with the same interface may be introduced into the system without the rest of the program needing to be changed.

CLOS makes it easier to design, develop, extend, and maintain a program. The larger the program, the more complex it is, and the more important it is to structure it into modules that minimize the dependencies between them. Use of object-oriented programming in CLOS supports this type of decomposition into separately maintainable and extensible components that interact only using interfaces.

Like the rest of Lisp, CLOS is a completely dynamic language that is runtime extensible without the need for all the source code. In the running application, classes can be redefined or extended, objects can change their class, and methods can be redefined or added. As you shall see later, important applications are built that rely on this feature.

To summarize, using CLOS in Lisp applications adds the following benefits:

  The application source code more directly models the application domain. The code reflects the abstract properties of objects and hides implementation details.
  Modules use interfaces to interact with each other. The interfaces encapsulate the implementation away from modules of the system that use the abstract operations and remain unchanged even when the implementation of a module changes.
  Applications are easily extensible, both in source and at runtime. Applications designed to be extensible allow users to extend a set of classes and operations on those classes. The supplied set of classes are used as a framework of building blocks and predefined abstract behavior that end users can use to create new classes that have application-specific custom behavior.
  CLOS is a standard. Multiple vendors supply CLOS. CLOS (or parts of it) is being used to add object orientation to other Lisp dialects such as EuLisp or Emacs Lisp.
  CLOS is implemented as a documented object-oriented protocol. This object protocol (MOP) allows the implementation of different object-oriented semantics, typically specialized to a problem domain. Language research and embedding other language semantics directly in Lisp are two different such uses of the MOP.

In keeping with the Lisp tradition of extensibility and openness, CLOS makes no attempt to enforce modularity or to hide implementation across module boundaries. Instead, it encourages application programmers to use CLOS to design modular and extensible programs.

5.3. Components of CLOS

CLOS programs consist of classes, instances, generic functions, and methods. CLOS programs are put together with inheritance, method combination, and, of course, regular procedural Common Lisp code. I’ll introduce these elements in this section and occasionally contrast the terminology and use against a couple other popular object-oriented languages: C++ and Smalltalk.

For a good introductory book on both CLOS and object-oriented programming, see Object-Oriented Programming in Common Lisp (Keene, 1989).


Previous Table of Contents Next