HOME

TheInfoList



OR:

''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a
software engineering Software engineering is a systematic engineering approach to software development. A software engineer is a person who applies the principles of software engineering to design, develop, maintain, test, and evaluate computer software. The term '' ...
book describing
software design pattern In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine ...
s. The book was written by
Erich Gamma Erich Gamma is a Swiss computer scientist and one of the four co-authors (referred to as "Gang of Four") of the software engineering textbook, '' Design Patterns: Elements of Reusable Object-Oriented Software''. Gamma is an expert in the Eclipse ...
, Richard Helm, Ralph Johnson, and John Vlissides, with a foreword by
Grady Booch Grady Booch (born February 27, 1955) is an American software engineer, best known for developing the Unified Modeling Language (UML) with Ivar Jacobson and James Rumbaugh. He is recognized internationally for his innovative work in software archi ...
. The book is divided into two parts, with the first two chapters exploring the capabilities and pitfalls of object-oriented programming, and the remaining chapters describing 23 classic
software design pattern In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine ...
s. The book includes examples in
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
and
Smalltalk Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan ...
. 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).


History

The book started at a birds of a feather (BoF) session at
OOPSLA OOPSLA (Object-Oriented Programming, Systems, Languages & Applications) is an annual ACM research conference. OOPSLA mainly takes place in the United States, while the sister conference of OOPSLA, ECOOP, is typically held in Europe. It is ope ...
'90, "Towards an Architecture Handbook", run by Bruce Anderson, where Erich Gamma and Richard Helm met and discovered their common interest. They were later joined by Ralph Johnson and John Vlissides. The original publication date of the book was October 21, 1994 with a 1995 copyright, hence it is often cited with a 1995-year, despite being published in 1994. The book was first made available to the public at the OOPSLA meeting held in Portland, Oregon, in October 1994. In 2005 the ACM
SIGPLAN SIGPLAN is the Association for Computing Machinery's Special Interest Group on programming languages. Conferences * Principles of Programming Languages (POPL) * Programming Language Design and Implementation (PLDI) * International Symposium on ...
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 design". As of March 2012, the book was in its 40th printing.


Introduction

Chapter 1 is a discussion of
object-oriented Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
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 Composition over inheritance (or composite reuse principle) in object-oriented programming (OOP) is the principle that classes should achieve polymorphic behavior and code reuse by their composition (by containing instances of other classes th ...
: "Favor '
object composition In computer science, object composition and object aggregation are closely related ways to combine objects or data types into more complex ones. In conversation the distinction between composition and aggregation is often ignored. Common kind ...
' over ' class inheritance'." (Gang of Four 1995:20) The authors claim the following as advantages of
interfaces Interface or interfacing may refer to: Academic journals * ''Interface'' (journal), by the Electrochemical Society * '' Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics'' * '' Int ...
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 Dynamic binding may refer to: * Dynamic binding (computing), also known as late binding * Dynamic scoping in programming languages * Dynamic binding (chemistry) See also * Dynamic dispatch *Dynamic linking In computing, a dynamic linker is the p ...
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. Of ...
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 computer science, object composition and object aggregation are closely related ways to combine objects or data types into more complex ones. In conversation the distinction between composition and aggregation is often ignored. Common kind ...
(in which objects with well-defined interfaces are used dynamically at runtime by objects obtaining references to other objects) as ''
black-box In science, computing, and engineering, a black box is a system which can be viewed in terms of its inputs and outputs (or transfer characteristics), without any knowledge of its internal workings. Its implementation is "opaque" (black). The te ...
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 Generic or generics may refer to: In business * Generic term, a common name used for a range or class of similar things not protected by trademark * Generic brand, a brand for a product that does not have an associated brand or trademark, other ...
(Ada, Eiffel,
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
, C#, VB.NET, and Delphi) 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 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 A framework is a generic term commonly referring to an essential supporting structure which other things are built on top of. Framework may refer to: Computing * Application framework, used to implement the structure of an application for an op ...
' 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 The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes. In normal usage, the client software creates a concrete implementation of the abstract fa ...
groups object factories that have a common theme. * Builder constructs complex objects by separating construction and representation. *
Factory method In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by c ...
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 programming. A prototype is generally used to ...
creates objects by cloning an existing object. * Singleton restricts object creation for a class to only one instance.


Structural

These concern class and object composition. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality. *
Adapter An adapter or adaptor is a device that converts attributes of one electrical device or system to those of an otherwise incompatible device or system. Some modify power or signal attributes, while others merely adapt the physical form of one c ...
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 a physical obstacle (such as a body of water, valley, road, or rail) without blocking the way underneath. It is constructed for the purpose of providing passage over the obstacle, which is usually someth ...
decouples an abstraction from its implementation so that the two can vary independently. *
Composite Composite or compositing may refer to: Materials * Composite material, a material that is made from several different substances ** Metal matrix composite, composed of metal and other parts ** Cermet, a composite of ceramic and metallic materials ...
composes zero-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. * 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 above 49 kg (108 lb) and up to 51 kg (112 lb). Professional boxing The flyweight division was the last of bo ...
reduces the cost of creating and manipulating a large number of similar objects. *
Proxy Proxy may refer to: * Proxy or agent (law), a substitute authorized to act for another entity or a document which authorizes the agent so to act * Proxy (climate), a measured variable used to infer the value of a variable of interest in climate re ...
provides a placeholder for another object to control access, reduce cost, and reduce complexity.


Behavioral

Most of these design patterns are specifically concerned with communication between objects. *
Chain of responsibility The chain of responsibility is a policy concept used in Australian transport legislation to place legal obligations on parties in the transport supply chain or across transport industries generally. The concept was initially developed to appl ...
delegates commands to a chain of processing objects. *
Command Command may refer to: Computing * Command (computing), a statement in a computer language * COMMAND.COM, the default operating system shell and command-line interpreter for DOS * Command key, a modifier key on Apple Macintosh computer keyboards * ...
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 Mediator may refer to: *A person who engages in mediation * Business mediator, a mediator in business * Vanishing mediator, a philosophical concept * Mediator variable, in statistics Chemistry and biology *Mediator (coactivator), a multiprotein ...
allows
loose coupling In computing and systems design, a loosely coupled system is one # in which components are weakly associated (have breakable relationships) with each other, and thus changes in one component least affect existence or performance of another comp ...
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 An observer is one who engages in observation or in watching an experiment. Observer may also refer to: Computer science and information theory * In information theory, any system which receives information from an object * State observer in co ...
is a publish/subscribe pattern, which allows a number of observer objects to see an event. *
State State may refer to: Arts, entertainment, and media Literature * ''State Magazine'', a monthly magazine published by the U.S. Department of State * ''The State'' (newspaper), a daily newspaper in Columbia, South Carolina, United States * ''Our S ...
allows an object to alter its behavior when its internal state changes. *
Strategy Strategy (from Greek στρατηγία ''stratēgia'', "art of troop leader; 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 " ...
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 A visitor, in English and Welsh law and history, is an overseer of an autonomous ecclesiastical or eleemosynary institution, often a charitable institution set up for the perpetual distribution of the founder's alms and bounty, who can inter ...
separates an algorithm from an object structure by moving the hierarchy of methods into one object.


Criticism

Criticism has been directed at the concept of
software design pattern In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine ...
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 Peter Norvig (born December 14, 1956) is an American computer scientist and Distinguished Education Fellow at the Stanford Institute for Human-Centered AI. He previously served as a director of research and search quality at Google. Norvig is t ...
demonstrates that 16 out of the 23 patterns in ''Design Patterns'' are simplified or eliminated by language features in
Lisp A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lispin ...
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. There has also been humorous criticism, such as a show trial at OOPSLA '99 on 3 November 1999, and a parody of the format, by Jim Coplien, entitled
Kansas City Air Conditioner
. 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 is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine ...
*
Enterprise Integration Patterns ''Enterprise Integration Patterns'' is a book by Gregor Hohpe and Bobby Woolf and describes 65 patterns for the use of enterprise application integration and message-oriented middleware in the form of a pattern language. The integration (messa ...
*
GRASP (object-oriented design) General Responsibility Assignment Software Patterns (or Principles), abbreviated GRASP, is a set of "nine fundamental principles in object design and responsibility assignment" first published by Craig Larman in his 1997 book ''Applying UML an ...
* Pedagogical patterns


Notes


References

{{DEFAULTSORT:Design Patterns (Book) Software engineering books Software design patterns 1994 non-fiction books Addison-Wesley books