Design Patterns: Not To Be Confused With The Concept of A
Design Patterns: Not To Be Confused With The Concept of A
Design Patterns:
Author(s)
Erich Gamma,
Richard Helm,
Ralph Johnson,
John Vlissides
Country
USA
Subject(s)
Publisher
Addison-Wesley
Publication date
1994
Pages
395
ISBN
0-201-63361-2
OCLCNumber
31171684
Dewey Decimal
005.1/2 20
LC Classification
Design Patterns: Elements of Reusable Object-Oriented Software is a software engineering book describing recurring
solutions to common problems in software design. The book's authors are Erich Gamma, Richard Helm, Ralph
Johnson and John Vlissides with a foreword by Grady Booch. The authors are often referred to as the Gang of Four, GoF,
[1]
or Go4. 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 patterns. The book includes
examples in C++ and Smalltalk.
The original publication date of the book was October 21, 1994 with a 1995 copyright, and as of March 2011, the book
was in its 39th printing. The book was first made available to the public at OOPSLA meeting held in Portland, Oregon, in
October 1994. It has been highly 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.
Contents
[show]
[edit]Introduction,
Chapter 1
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:
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 is consequentially important to objectoriented programming.
The authors refer to inheritance 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 welldefined 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 classesbut 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, C#,
VB.NET, and Delphi) or templates (C++). These allow any type to be defined without specifying all the other types it
usesthe 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 aren't 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' 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.
[edit]Case
study, Chapter 2
The chapter goes through seven problems that must be addressed in order to properly design Lexi, including any
constraints that must be followed. Each problem is analyzed in depth, and solutions are proposed. Each solution
is explained in full, including pseudo-code and a slightly modified version of Object Modeling Technique where
appropriate.
Finally, each solution is associated directly with one or more design patterns. It is shown how the solution is a
direct implementation of that design pattern.
The seven problems (including their constraints) and their solutions (including the pattern(s) referenced), are as
follows:
[edit]Document
Structure
The document is "an arrangement of basic graphical elements" such as characters, lines, other shapes, etc., that
"capture the total information content of the document"(pp35). The structure of the document contains a
collection of these elements, and each element can in turn be a substructure of other elements.
Problems and Constraints
1.
Text and graphics should be treated the same way (that is, graphics aren't a derived instance of text,
nor vice versa)
2.
The implementation should treat complex and simple structures the same way. It should not have to
know the difference between the two.
3.
Formatting differs from structure. Formatting is a method of constructing a particular instance of the document's
physical structure. This includes breaking text into lines, using hyphens, adjusting for margin widths, etc.
Problems and Constraints
1.
2.
The ability to change the graphical interface that the user uses to interact with the document.
Problems and Constraints
1.
2.
Scroll bars that let the user view different parts of the page
3.
4.
Avoid an "explosion of classes" that would be caused by subclassing for "every possible combination
of embellishments" and elements (p44)
[edit]Supporting
Look-and-feel refers to platform-specific UI standards. These standards "define guidelines for how applications
appear and react to the user" (pp47).
Problems and Constraints
1.
2.
3.
4.
Have a set of abstract elemental subclasses for each category of elements (ScrollBar, Buttons, etc.)
5.
Have a set of concrete subclasses for each abstract subclass that can have a different look-and-feel
standard. (ScrollBar having MotifScrollBar and PresentationScrollBar for Motif and Presentation
look-and-feels)
Just as look-and-feel is different across platforms, so is the method of handling windows. Each platform
displays, lays out, handles input to and output from, and layers windows differently.
Problems and Constraints
1.
The document editor must run on many of the "important and largely incompatible window systems"
that exist (p. 52)
2.
An Abstract Factory cannot be used. Due to differing standards, there will not be a common abstract
class for each type of widget.
3.
All actions the user can take with the document, ranging from entering text, changing formatting, quitting,
saving, etc.
Problems and Constraints
1.
Operations must be accessed through different inputs, such as a menu option and a keyboard shortcut
for the same command
2.
3.
4.
In order to avoid coupling, there must not be a lot of dependencies between implementation and user
interface classes.
5.
Undo and redo commands must be supported on most document changing operations, with no
arbitrary limit on the number of levels of undo
6.
Functions are not viable, since they don't undo/redo easily, are not easily associated with a state, and
are hard to extend or reuse.
7.
Menus should be treated like hierarchical composite structures. Hence, a menu is a menu item that
contains menu items which may contain other menu items, etc.
This is the document editor's ability to textually analyze the contents of a document. Although there are many
analyses that can be performed, spell check and hyphenation-formatting are the focus.
Problems and Constraints
1.
Allow for multiple ways to check spelling and identify places for hyphenation
2.
Allow for expansion for future analysis (e.g., word count, grammar check)
3.
Be able to iterate through a text's contents without access to the text's actual structure (e.g., array,
linked list, string)
4.
Allow for any manner of traversal of document (beginning to end, end to beginning, alphabetical
order, etc.)
In addition, the algorithm themselves can be responsible for dealing with different elements. For example,
a SpellCheck algorithm would ignore a Graphic element, rather than having to program
every Graphic-derived element to not send themselves to a SpellCheck.
[edit]Patterns
by Type
[edit]Creational
Creational patterns are ones that create objects for you, rather than having you instantiate objects directly. This
gives your program more flexibility in deciding which objects need to be created for a given case.
Factory Method creates objects without specifying the exact class to create.
[edit]Structural
These 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 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.
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.
[edit]Behavioral
Most of these design patterns are specifically concerned with communication between objects.
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.
Template method defines the skeleton of an algorithm as an abstract class, allowing its subclasses to
provide concrete behavior.
[edit]
Visitor separates an algorithm from an object structure by moving the hierarchy of methods into one object.