''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a
software engineering
Software engineering is a branch of both computer science and engineering focused on designing, developing, testing, and maintaining Application software, software applications. It involves applying engineering design process, engineering principl ...
book describing
software design pattern
In software engineering, a software design pattern or design pattern is a general, reusable solution to a commonly occurring problem in many contexts in software design. A design pattern is not a rigid structure to be transplanted directly into s ...
s. The book was written by
Erich Gamma,
Richard Helm,
Ralph Johnson, and
John Vlissides, with a foreword by
Grady Booch. The book is divided into two parts, with the first two chapters exploring the capabilities and pitfalls of
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
, and the remaining chapters describing 23 classic
software design pattern
In software engineering, a software design pattern or design pattern is a general, reusable solution to a commonly occurring problem in many contexts in software design. A design pattern is not a rigid structure to be transplanted directly into s ...
s. The book includes examples in
C++ and
Smalltalk
Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learni ...
.
It has been influential to the field of software engineering and is regarded as an important source for object-oriented design theory and practice. More than 500,000 copies have been sold in English and in 13 other languages.
The authors are often referred to as the ''Gang of Four'' (GoF).
Development and publication history
The book started at a birds-of-a-feather session at the 1990
OOPSLA meeting, "Towards an Architecture Handbook", where Erich Gamma and Richard Helm met and discovered their common interest. They were later joined by Ralph Johnson and John Vlissides. The book was originally published on 21 October 1994, with a 1995 copyright, and was made available to the public at the 1994 OOPSLA meeting.
Introduction
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." (Gang of Four 1995:18)
*
Composition over inheritance: "Favor '
object composition' over '
class inheritance'." (Gang of Four 1995:20)
The authors claim the following as advantages of
interfaces over implementation:
* 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
Use of an interface also leads to
dynamic binding and
polymorphism, which are central features of object-oriented programming.
The authors refer to
inheritance
Inheritance is the practice of receiving private property, titles, debts, entitlements, privileges, rights, and obligations upon the death of an individual. The rules of inheritance differ among societies and have changed over time. Offi ...
as ''
white-box reuse'', with white-box referring to visibility, because the internals of parent classes are often visible to
subclasses. In contrast, the authors refer to
object composition (in which objects with well-defined interfaces are used dynamically at runtime by objects obtaining references to other 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 (Gang of Four 1995:20). The danger is stated as follows:
:"Because inheritance exposes a
subclass to details of its parent's implementation, it's often said that 'inheritance breaks encapsulation'". (Gang of Four 1995:19)
They warn that the implementation of a subclass can become so bound up with the implementation of its parent class that any change in the parent's implementation will force the subclass to change. Furthermore, they claim that a way to avoid this is to inherit only from abstract classes—but then, they point out that there is minimal code reuse.
Using inheritance is recommended mainly when adding to the functionality of existing components, reusing most of the old code and adding relatively small amounts of new code.
To the authors, 'delegation' is an extreme form of object composition that can always be used to replace inheritance. Delegation involves two objects: a 'sender' passes itself to a 'delegate' to let the delegate refer to the sender. Thus the link between two parts of 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
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
,
C#,
Visual Basic (.NET), and
Delphi
Delphi (; ), in legend previously called Pytho (Πυθώ), was an ancient sacred precinct and the seat of Pythia, the major oracle who was consulted about important decisions throughout the ancient Classical antiquity, classical world. The A ...
) or templates (
C++). These allow any 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 understand and build than more static software." (Gang of Four 1995: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) and acquaintance, 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 are not responsible for each other. Acquaintance is a weaker relationship than aggregation and suggests much
looser coupling between objects, which can often be desirable for maximum maintainability in designs.
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 that make 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 by type
Creational
Creational patterns are ones that create objects, rather than having to instantiate objects directly. This gives the program more flexibility in deciding which objects need to be created for a given case.
*
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 class to create.
*
Prototype
A prototype is an early sample, model, or release of a product built to test a concept or process. It is a term used in a variety of contexts, including semantics, design, electronics, and Software prototyping, software programming. A prototype ...
creates objects by cloning an existing object.
*
Singleton restricts object creation for a class to only one instance.
Structural
Structural patterns concern class and object composition. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.
*
Adapter allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class.
*
Bridge
A bridge is a structure built to Span (engineering), span a physical obstacle (such as a body of water, valley, road, or railway) without blocking the path underneath. It is constructed for the purpose of providing passage over the obstacle, whi ...
decouples an abstraction from its implementation so that the two can vary independently.
*
Composite composes zero-or-more similar objects so that they can be manipulated as one object.
*
Decorator dynamically adds/overrides behavior in an existing method of an object.
*
Facade provides a simplified interface to a large body of code.
*
Flyweight
Flyweight is a weight class in combat sports.
Boxing
Flyweight is a class in boxing which includes fighters weighing up to and including 51 kg (112 lb) for a title fight.
Professional boxing
The flyweight division was the last of boxin ...
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.
Behavioral
Most
behavioral design patterns are specifically concerned with communication between objects.
*
Chain of responsibility delegates commands to a chain of processing objects.
*
Command creates objects that 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
Strategy (from Greek στρατηγία ''stratēgia'', "troop leadership; office of general, command, generalship") is a general plan to achieve one or more long-term or overall goals under conditions of uncertainty. In the sense of the " a ...
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.
Reception
In 2005 the ACM
SIGPLAN
SIGPLAN is the Association for Computing Machinery's Special Interest Group (SIG) on programming languages. This SIG explores programming language concepts and tools, focusing on design, implementation, practice, and theory. Its members are progra ...
awarded that year's Programming Languages Achievement Award to the authors, in recognition of the impact of their work "on programming practice and
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
design".
Criticism has been directed at the concept of
software design pattern
In software engineering, a software design pattern or design pattern is a general, reusable solution to a commonly occurring problem in many contexts in software design. A design pattern is not a rigid structure to be transplanted directly into s ...
s generally, and at ''Design Patterns'' specifically. A primary criticism of ''Design Patterns'' is that its patterns are simply workarounds for missing features in C++, replacing elegant abstract features with lengthy concrete patterns, essentially becoming a "human compiler".
Paul Graham wrote:
[
]
Peter Norvig demonstrates that 16 out of the 23 patterns in ''Design Patterns'' are simplified or eliminated by language features in
Lisp
Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation.
Originally specified in the late 1950s, ...
or
Dylan.
Related observations were made by Hannemann and
Kiczales who implemented several of the 23 design patterns using an
aspect-oriented programming language (
AspectJ) and showed that code-level dependencies were removed from the implementations of 17 of the 23 design patterns and that aspect-oriented programming could simplify the implementations of design patterns.
In an interview with InformIT in 2009, Erich Gamma stated that the book authors had a discussion in 2005 on how they would have refactored the book and concluded that they would have recategorized some patterns and added a few additional ones, such as extension object/interface, dependency injection, type object, and null object. Gamma wanted to remove the singleton pattern, but there was no consensus among the authors to do so.
See also
*
Software design pattern
In software engineering, a software design pattern or design pattern is a general, reusable solution to a commonly occurring problem in many contexts in software design. A design pattern is not a rigid structure to be transplanted directly into s ...
*
Enterprise Integration Patterns
*
GRASP (object-oriented design)
*
Pedagogical patterns
References
{{DEFAULTSORT:Design Patterns (Book)
Software engineering books
Software design patterns
1994 non-fiction books
Addison-Wesley books