Much Ado About Patterns

by Robert Zubek

Introduction

It is no news that patterns abound in all areas of human endeavor. In programming in particular, one can often find certain regularities in the way things are done -- there are canonical and time-tested ways of designing compilers, operating systems, and many other types of complex software. On a lower level, there are also many regular ways of dealing with recurring problems, such as that of how to represent complicated data structures in memory. Indeed, it seems that everywhere we look in computer programming, we are surrounded by regularities and standards.

Considering all this, it might seem that the recent excitement over patterns is just ``much ado about nothing'' -- after all, haven't we been using patterns in software development all along?

After taking a closer look, we notice that that is not the case. What causes a stir in the software development community is not just regular patterns in code, in the sense of simple regularity and repeatability. Rather it is the more abstract concept of a pattern as a formalized way of recording experience. This new way of thinking about patterns was introduced by Christopher Alexander, an architect who used patterns in architecture and urban planning -- he introduced the idea in [1], his 1964 book, and developed it further in later works such as [2]. The immense potential of patterns has recently been recognized in the software community as well, mainly because they can be used to simplify the creation of complex systems.

If the concept of a formalized pattern seems a bit puzzling, it probably should, because patterns -- in this new sense -- can be powerful beyond expectations. We will, for example, find that such patterns not only can provide us with solutions to specific problems (which is what recognizing regularities in code can help us to do), but can also teach us how to deal with similar problems in the future, or even help encode information about the design of the program in its implementation -- embed explanation of code into code itself!

In this article, I would like to present and explain patterns, with special emphasis on design patterns, a type of pattern often used in software design. We will begin with a brief overview of what patterns are -- just enough to get us going, because a more detailed definition might be a bit too abstract at first. Then we will see a few examples of patterns -- what they are, how they are used, and when they can be useful. After that we will return to the problem of defining a pattern, and examine the more detailed definitions of patterns, as well as some of their common characteristics.

What Are Patterns? (Part I)

The fundamental question ``what are patterns?'' has no absolute answer -- there is no exact definition. Were it otherwise, Alexander would have been able to describe them in a sentence or a paragraph, rather than the several books that he has written so far. There are some generalizations that can be made, however, and the people who work on patterns have more or less agreed on a certain way of defining them. We will delay a full definition until we see some examples; for our immediate purposes, we can use a popular simplified definition, which says that a pattern is a solution to a problem in a recurring context. This means that a pattern is a bit of formalized experience in dealing with particular recurring situations. However, all three elements of the definition are necessary: the solution has to be provided, the problem must exist, and the context must not be unique. This definition is not to be understood from the first reading -- it's just something to keep in mind while reading examples.

What Are Design Patterns?

Much of what is being said about patterns in software development pertains to design patterns, which are a group of patterns that apply to the task of creating software systems. The label ``design patterns'' is somewhat misleading, considering that it can be used to describe any patterns involved in designing anything (not just software). However, the label was used in some highly influential works (such as [4]), and it remained. [4] is also the source of a very useful definition of design patterns: ``descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.''

Sample Design Patterns

The following examples represent design patterns in object-oriented programming. As you read through them, notice certain regularities between them. These patterns have been introduced in [4], from which I took their definitions. Note that due to the introductory nature of this article, as well as space constraints, these patterns have been shortened and simplified. After all, it took Gamma et al 268 pages to explain 23 patterns!

These examples are in C++ (or pseudo-C++), and they require some knowledge of the language and object-oriented design in general. They should not be hard to replicate in other object-oriented languages (though one must remember that while patterns are language independent, not all languages are powerful enough to make using patterns worthwhile [5]).

While reading, you might find it useful to jump between the sample code and its description. Figure 1 will explain the notation used.

Example 1: Strategy [4]

Intent:

Motivation: Structure: Example: Consequences:

Example 2: Singleton [4]

Intent:

Motivation: Structure: Example: Consequences:

What Are Patterns? (Part II)

Now that we've seen some examples of patterns, let's take a look back at our working definition. Thinking of a pattern as a solution to a problem in a recurring context now might seem a bit problematic. First of all, the emphasis is on a solution, but a pattern is more than that -- it does not just provide a cookbook answer, a trick, or an algorithm, but instead it teaches how to deal with similar (and not-so-similar) situations in the future. One of the lessons the Singleton pattern teaches, for example, is that whenever we don't want the user to create an instance of a class directly, we can hide the constructor behind the private statement, and provide some class method for instantiation.

Second, problem is defined too narrowly. In the Strategy pattern, for example, there was no single problem to make us use the pattern instead of hard-coding the algorithms. Rather, there was a set of constraints, or forces, that made the pattern more beneficial -- forces such as the ease of maintenance, reduction of memory usage, or extensibility. Thus, we must understand that the problem is not a single entity, but rather a set of forces that often include the need for correctness, optimized resource usage, structure, extensibility, and so on [7].

Third, the context must refer to a recurring set of situations in which the forces apply. The pattern would have no value if the situation in which it is applicable occurred only once in a thousand years -- the time span has to be short enough to allow people not only to notice a good pattern, but also to benefit from applying it during the next occurrence of the context. Similarly, it has to be the context in which the same set of forces applies -- a pattern is no good if the context recurs but the forces keep changing.

It is advantageous to find unique names for patterns. These names will serve to distinguish between individual patterns and can also serve to form a dictionary of concepts that developers might use when designing systems and communicating [9]. The names therefore form a ``pattern language'' that enhances the developer's ability to reason about complex problems.

Thus, we can use the above definition only if we appropriately expand the terms ``solution'', ``problem'', and ``context''. Unfortunately, there are no definitions that would compress these qualifiers into a short sentence. However, a longer and comprehensive definition would do quite well, such as the following one suggested by Appleton in [3]:

A pattern is a named nugget of instructive information that captures the essential structure and insight of a successful family of proven solutions to a recurring problem that arises within a certain context and a system of forces.
While the pattern community has not agreed on one precise definition, as long as we keep all these qualifiers in mind, we will have a good idea of what patterns are.

How Can I Benefit From Patterns?

There are several ways in which using patterns is beneficial to all types of programmers. The most obvious benefit is that patterns provide solution to problems, so they save development time. Patterns are also ways of recording experience, and thus learning patterns improves (or solidifies) one's skills in that area.

A less obvious characteristic of patterns, which -- as many developers stress -- is nevertheless very important, is that a set of patterns creates a shared common vocabulary among developers. Using this ``pattern language'', developers are able to share experience and communicate effectively about high-level concepts, which is very beneficial considering the complexity of large software projects. By presenting a common set of metaphors, patterns work also in creating a common way of thinking about problems and solutions in software development.

As for design patterns in particular, they can make code easier to read. By recognizing a pattern in code, one sees not only a solution to a problem, but also the set of forces and the context in which they exist. Thus, when the reader of the code notices a design pattern, he or she also gets some information about the relationships between the parts of the system that led to this pattern being beneficial; indeed, it seems that some description of the design has been transferred into the code by using the pattern.

Conclusion

Patterns in general, and design patterns in particular, have become a very discussed topic in the fields related to software development. They represent many of the things we, as software developers, are looking for: easier (and more formalized) ways of sharing experience, simplifications of complex systems, and ways of writing code so that it also conveys information about the overall design. Their popularity, however sudden, is well deserved.

Patterns represent a big shift in software design, which should make programming not merely more efficient, but also more clear.

Pointers

It is my hope that after reading this article you became at least interested in (if not completely fascinated by) patterns. If you would like to learn more about design patterns, as used in software development, a very highly regarded book is [4], and titles of more books can be found in [6]. Many links to online documents about patterns in general, such as [3], [7] or [9], can also be found on the Pattern Homepage (hillside.net/patterns/patterns.html)

Acknowledgements

Special thanks go to Bryan Quinn for his extensive comments on the draft of this article.

References

1
Alexander, C. Notes on the Synthesis of Form. Harvard University Press, Cambridge, Mass., 1964.
2
Alexander, C., Sara Ishikawa, and Murray Silverstein. A Pattern Language: Towns, Buildings, Construction. Oxford University Press, New York, New York, 1977.
3
Appleton, B. Patterns and Software: Essential Concepts and Terminology. http://www.enteract.com/~bradapp/docs/patterns-intro.html
4
Gamma, E., Helm, R., Johnson R., and Vlissides J. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, Reading, Mass., 1995.
5
Grosso, W. Dynamic Design Patterns in Objective-C. Dr. Dobb's Journal, 268 (Aug. 1997), p. 38.
6
Kaplan, J. Patterns Reading List. Object Magazine (Mar. 1997), http://www.sigs.com/publications/docs/objm/9703/9703.patterns.html
7
Lea, D. Patterns-Discussion FAQ. http://g.oswego.edu/dl/pd-FAQ/pd-FAQ.html
8
Stroustrup, B. The C++ Programming Language, 3rd Ed. Addison-Wesley, Reading, Mass., 1997.
9
Vlissides, J. Patterns: The Top Ten Misconceptions. Object Magazine (Mar. 1997), http://www.sigs.com/publications/docs/objm/9703/9703.vlissides.html
Robert Zubek is a junior at Northwestern University, majoring in computer science, and a co-founder and past president of the local ACM student chapter. His current activities include advocating Perl, digging through Knuth's trilogy, and dreaming about all the software he could have written if he didn't have to sleep -- but above all, he enjoys spending time with friends and family.

Want more Crossroads articles about Object-Oriented Programming? Go to the index or the previous one.

Want more Crossroads articles about Software Engineering? Go to the index or to the next one or to the previous one.

Copyright 1998 Robert Zubek
Last Modified:
Location: www.acm.org/crossroads/xrds5-1/patterns.html