Design Patterns: Elements of Reusable Object-Oriented Software (ISBN 0-201-63361-2) is a landmark book, first published in 1995, that recommends a set of best practices for object-oriented design and catalogs a variety of object-oriented software architectures illustrated in C++ and Smalltalk. In its 36th printing as of April 2007, the book has been translated into more than a dozen languages and has been highly regarded in the field of software engineering. However, the book makes for such dense reading (even for experienced programmers) that it has been superseded, in practice, by a spate of more recent, accessibly-written books despite being regarded as an important source for object-oriented design theory.
Overview and philosophy
Chapter 1 is a discussion of object-oriented design techniques, based on the authors' experience, which they believe would lead to good object-oriented software design, including:
- "Program to an 'interface', not an 'implementation'." (page 18)
- "Favor 'object composition' over 'class inheritance'." (page 20)
- clients remain unaware of the specific types of objects they use, as long as the object adheres to the interface
- clients remain unaware of the classes that implement these objects; clients only know about the abstract class(es) defining the interface
The authors refer to inheritance as white-box reuse, withwhite-box referring to visibility, because the internals of parent classes areoften visible to subclasses.In contrast, the authors refer to object composition (in which objectswith well-defined interfaces are used dynamically at runtime by objects obtaining references toother objects) as black-box reuse because no internal details of composed objects need be visible in the code using them.
The authors discuss the tension between inheritance and encapsulation at length and state that in their experience, designers overuse inheritance (page 20). The danger is stated as follows:
They warn that the implementation ofa subclass can become so bound up with the implementation of its parent class thatany change in the parent's implementation will force the subclass to change.They say that a way to avoid this is to inherit only from abstract classes--but then, they point out that there is minimal code reuse.
They recommend using inheritance mainly when adding to the functionalityof existing components, reusing most of the old code and adding relativelysmall amounts of new code.
To the authors, 'delegation' is an extreme form of object compositionthat can always be used to replace inheritance. Delegation involves twoobjects: a 'sender' passes itself to a 'delegate'to let the delegate refer to the receiver. Thus the link between two partsof a system are established only at runtime, not at compile-time. The Callback article has more information about delegation.
The authors also discuss so-called parameterized types, which are also known as generics (Ada, Eiffel, Java, C#) or templates (C++). These allow a type to be defined without specifying all the other types it uses--the unspecified types are supplied as 'parameters' at the point of use.
The authors admit that delegation and parameterization are very powerful but add a warning: "Dynamic, highly parameterized software is harder to understandthan more static software." (page 21)
The authors further distinguish between 'aggregation', where one object 'has' or 'is part of' another object(implying that an aggregate object and its owner have identical lifetimes) andacquaintance, where one object merely 'knows of' another object.Sometimes acquaintance is called 'association' or the 'using' relationship.Acquaintance objects may request operations of each other, but they aren'tresponsible for each other. Acquaintance is a weaker relationship thanaggregation and suggests much looser coupling between objects, whichcan often be desirable for maximum maintainability in a design.
The authors employ the term 'toolkit' where others might today use 'class library',as in C# or Java. In their parlance, toolkits are the object-oriented equivalent of subroutine libraries,whereas a 'framework' is a set of cooperating classes thatmake up a reusable design for a specific class of software.They state that applications are hard to design,toolkits are harder, and frameworks are the hardest to design.
Patterns covered in the book
- Abstract Factory groups object factories that have a common theme.
- Builder constructs complex objects by separating construction and representation.
- Factory Method creates objects without specifying the exact object to create.
- Prototype creates objects by cloning an existing object.
- Singleton restricts object creation for a class to only one instance.
- Adapter allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class.
- Bridge decouples an abstraction from its implementation so that the two can vary independently.
- Composite composes one-or-more similar objects so that they can be manipulated as one object.
- Decorator dynamically adds/overrides behaviour in an existing method of an object.
- Façade provides a simplified interface to a large body of code.
- Flyweight reduces the cost of creating and manipulating a large number of similar objects.
- Proxy provides a placeholder for another object to control access, reduce cost, and reduce complexity.
- Chain of responsibility delegates a series of commands to a chain of processing objects.
- Command creates objects which encapsulate actions and parameters.
- Interpreter implements a specialized language.
- Iterator accesses the elements of an object sequentially without exposing its underlying representation.
- Mediator allows loose coupling between classes by being the only class that has detailed knowledge of their methods.
- Memento provides the ability to restore an object to its previous state (undo).
- Observer is a publish/subscribe pattern which allows a number of observer objects to see an event.
- State allows an object to alter its behavior when its internal state changes.
- Strategy allows one of a family of algorithms to be selected on-the-fly at runtime.
- Template method defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior.
- Visitor separates an algorithm from an object structure by moving the hierarchy of methods into one object.
- Design Patterns: Elements of Reusable Object-Oriented Software Addison-Wesley, Publisher
- Pattern Hatching by John Vlissides (one of the gang of four)
- Anti Pattern Anti Pattern