Notes on Software Patterns:
Introduction to Patterns

H. Conrad Cunningham

16 April 2022

Copyright (C) 1998-2022, H. Conrad Cunningham
Professor of Computer and Information Science
University of Mississippi
214 Weir
P.O. Box 1848
University, MS 38677
(662) 915-7396 (dept. office)

Browser Advisory: The HTML version of this textbook requires a browser that supports the display of MathML. A good choice as of April 2022 is a recent version of Firefox from Mozilla.

1 Introduction to Patterns

1.1 Chapter Introduction

The goal of this chapter is to introduce the basic concepts and terminology of software patterns as used in software architecture, software engineering, and programming.

The chapter approaches patterns mostly from the perspective of object-oriented programming using languages such as Java and Scala. Classic works on software “design patterns” include the Gamma et al. (i.e., the “Gang of Four”) [3] and Buschmann et al. (i.e., “Siemens”) [2] books. In these notes, we primarily use the terminology of Buschmann et al. [2].

Similar approaches may be used in functional languages such as Haskell, but often functional languages will use first-class and higher-order functions to express the patterns.

The accompanying set of HTML slides (not fully updated in 2022) is the following:

1.2 What is a Pattern?

When experts need to solve a problem, they seldom invent a totally new solution. More often they will recall a similar problem they have solved previously and reuse the essential aspects of the old solution to solve the new problem. They tend to think in problem-solution pairs.

Identifying the essential aspects of specific problem-solution pairs leads to descriptions of problem-solving patterns that can be reused.

The concept of a pattern as used in software architecture is borrowed from the field of (building) architecture, in particular from the writings of architect Christopher Alexander [1]. Buschmann et al. [2] defines pattern as follows in the context of software architecture:

“A pattern for software architecture describes a particular recurring design problem that arises in specific design contexts and presents a well-proven generic scheme for its solution. The solution scheme is specified by describing its constituent components, their responsibilities and relationships, and the ways in which they collaborate.”

Where software architecture is concerned, the concept of a pattern described here is essentially the same concept as an architectural style or architectural idiom in Shaw and Garlan [9].

In general, patterns have the following characteristics [2]:

1.3 Descriptions of Patterns

Various authors use different formats (i.e., “languages”) for describing patterns. Typically a pattern will be described with a schema that includes at least the following three elements [2]:

  1. Context

  2. Problem

  3. Solution

1.3.1 Context

The Context element describes the circumstances in which the problem arises.

1.3.2 Problem

The Problem element describes the specific problem that arises repeatedly in the context.

In particular, the description describes the set of forces repeatedly arising in the context. A force is some aspect of the problem that must be considered when attempting a solution. Example types of forces include:

Forces may complementary (i.e., can be achieved simultaneously) or contradictory (i.e., can only be balanced).

1.3.3 Solution

The Solution section describes a proven solution to the problem.

The solution specifies a configuration of elements to balance the forces associated with the problem.

1.3.4 Aside: Extending pattern elements

In a helpful tutorial on pattern writing, Wellhausen and Fiesser [13] separate out the Forces and Consequence as separate element and state that the following elements must be present in the given order:

All of the above elements except Consequences are also prescribed by the Mandatory Elements Present pattern from Meszaros and Doble’s A Pattern Language for Pattern Writing [4]. Their Optional Elements When Helpful pattern suggests use of optional elements such as Examples, Code Samples, and Rationale. Following their Readable References to Patterns pattern, we refer to a pattern using its name in small capital letters.

1.4 Categories of Patterns

Patterns can be grouped into three categories according to their level of abstraction [2]:

  1. Architectural patterns

  2. Design patterns

  3. Idioms

1.4.1 Architectural patterns

Buschmann et al. [2] defines an architectural pattern as follows:

“An architectural pattern expresses a fundamental structural organization schema for software systems. It provides a set of predefined subsystems, specifies their responsibilities, and includes rules and guidelines for organizing the relationships between them.”

In early work, Shaw and Garlan [9] used the term architectural style instead of architectural pattern.

An architectural pattern is a high-level abstraction. The choice of the architectural pattern to be used is a fundamental design decision in the development of a software system. It determines the system-wide structure and constrains the design choices available for the various subsystems. It is, in general, independent of the implementation language used.

Examples of architectural patterns include the following.

The Buschmann et al. book [2], Qian et al. book [5], and Shaw paper [9] elaborate on these and other architectural patterns. (This link is to a local copy of a preprint of the Shaw paper.)

1.4.2 Design patterns

Buschmann et al. [2] defines design pattern as follows:

“A design pattern provides a scheme for refining the subsystems or components of a software system, or the relationships between them. It describes a commonly-recurring structure of communicating components that solves a general design problem within a particular context.”

A design pattern is a mid-level abstraction. The choice of a design pattern does not affect the fundamental structure of the software system, but it does affect the structure of a subsystem. Like the architectural pattern, the design pattern tends to be independent of the implementation language to be used.

A design pattern might not, however, be independent of the programming paradigm. A pattern that is meaningful for a statically typed, object-oriented language without first-class functions may not be as meaningful for a dynamically typed language or for a functional language with first-class functions.

Examples of design patterns include the following.

1.4.3 Idioms

Buschann et al. [2] defines idiom as follows:

“An idiom is a low-level pattern specific to a programming language. An idiom describes how to implement particular aspects of components or the relationships between them using the features of the given language.”

An idiom is a low-level abstraction. It is usually a language-specific pattern that deals with some aspects of both design and implementation.

In some sense, use of a consistent program coding and formatting style can be considered an idiom for the language being used. Such a style would provide guidelines for naming variables, laying out declarations, indenting control structures, ordering the features of a class, determining how values are returned, and so forth. A good style that is used consistently makes a program easier to understand than otherwise would be the case.

In Java, the language-specific iterator defined to implement the Iterator interface can be considered an idiom. It is a language-specific instance of the more general Iterator design pattern.

Another example of an idiom is the use of the Counted Pointer (or Counted Body or Reference Counting) technique for storage management of shared objects in C++. In this idiom, we control access to a shared object through two classes, a Body (representation) class and a Handle (access) class.

An object of the Body class holds the shared object and a count of the number of references to the object.

An object of a Handle class holds a direct reference to a body object; all other parts of the program must access the body indirectly through handle class methods. The handle methods can increment the reference count when a new reference is created and decrement the count when a reference is freed. When a reference count goes to zero, the shared object and its body can be deleted. Often the programmer using this pattern will want to override the operator-> of the handle class to give more transparent access to the shared object.

We can use a variant of the Counted Pointer idiom to implement a “copy on write” mechanism. That is, the body is shared as long as only “read” access is needed, but a copy is created whenever one of the holders makes a change to the state of the object.

1.5 What Next?

TODO

1.6 Acknowledgements

I wrote the first version of these notes for my Spring 1998 graduate Special Topics in Software Architecture class. I based the notes, in part, on:

I revised the notes somewhat for related courses in 1999, 2000, 2001, 2002, and 2004. Also, in 2004 I revised the notes, created slides, and included them as a part of the materials supported by a grant from the Acxiom Corporation titled “The Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE)”. My ALSACE research team included PhD students Yi Liu and Pallavi Tadepalli and MS students Mingxian Fu and “Melody” Hui Xiong.

In Spring 2017, I adapted the earlier notes to use Pandoc-flavored Markdown. In Spring 2018 I revised the notes slightly to fit in with the other documents for the CSci 658 course.

I retired from the full-time faculty in May 2019. As one of my post-retirement projects, I am continuing work on possible textbooks based on the course materials I had developed during my three decades as a faculty member. In January 2022, I began refining the existing content, integrating separately developed materials together, reformatting the documents, constructing a unified bibliography (e.g., using citeproc), and improving my build workflow and use of Pandoc.

In 2022, I also added the aside based on Wellhausen and Fiesser’s tutorial and expanded the discussion of the example patterns.

I maintain this chapter as text in Pandoc’s dialect of Markdown using embedded LaTeX markup for the mathematical formulas and then translate the document to HTML, PDF, and other forms as needed

1.7 Terms and Concepts

TODO: Update

Pattern (for software architecture), architectural pattern (or architectural style), design pattern, idiom. Pattern Context, Problem, and Solution. Pattern Name, Forces, and Consequences. Pipes and Filters, Layers, Blackboard, and Model-View-Controller architectural patterns. Adapter, Iterator, and Strategy design patterns. Counted Pointer (or Reference Counting, Handle-Body) idiom.

1.8 References

[1]
Christopher Alexander. 1977. A pattern language: Towns, buildings, construction. Oxford University Press, Oxford, UK.
[2]
Frank Buschmann, Regine Meunier, Hans Rohnert, Peter Sommerlad, and Michael Stal. 1996. Pattern-oriented software architecture: A system of patterns. Wiley, Hoboken, New Jersey, USA.
[3]
Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. 1995. Design patterns: Elements of reusable object-oriented software. Addison-Wesley, Boston, Massachusetts, USA.
[4]
Gerard Meszaros and Jim Doble. 1998. A pattern language for pattern writing. In Pattern languages of program design 3, Addison-Wesley, Boston, Massachusetts, USA, 529–574.
[5]
Kai Qian, Xiang Fu, Lixin Tao, Chong-Wei Xu, and Jorge L. Diaz-Herrera. 2010. Software architecture and design illuminated. Jones & Bartlett Learning, Burlington, Massachusetts, USA.
[6]
Dennis M. Ritchie. 1984. The UNIX system: The evolution of the UNIX time-sharing system. AT&T Bell Laboratories Technical Journal 63, 8 (1984), 1577–1593.
[7]
Hans Albrecht Schmid. 1996. Creating applications from components: A manufacturing framework design. IEEE Software 13, 6 (1996), 67–75.
[8]
Mary Shaw. 1996. Some patterns for software architecture. In Pattern languages of program design 2, John M. Vlissides, James O. Coplien and Norman L. Kerth (eds.). Addison-Wesley, Boston, Massachusetts, USA, 255–270.
[9]
Mary Shaw and David Garlan. 1996. Software architecture: Perspectives on an emerging discipline. Prentice-Hall, Englewood Cliffs, New Jersey, USA.
[10]
Source Making. 2022. Adapter design pattern. Retrieved from https://sourcemaking.com/design_patterns/adapter
[11]
Source Making. 2022. Iterator design pattern. Retrieved from https://sourcemaking.com/design_patterns/iterator
[12]
Source Making. 2022. Strategy design pattern. Retrieved from https://sourcemaking.com/design_patterns/strategy
[13]
Tim Wellhausen and Andreas Fiesser. 2011. How to write a pattern? A rough guide for first-time pattern authors. In Proceedings of the 16th Europe an conference on pattern languages of programs (EuroPLOP ’11), Irsee, Germany, 1–9 (Article 5).
[14]
Wikpedia: The Free Encyclopedia. 2022. Model-view-controller. Retrieved from https://en.wikipedia.org/wiki/Model-view-controller
[15]
Wikpedia: The Free Encyclopedia. 2022. Pipeline (software). Retrieved from https://en.wikipedia.org/wiki/Pipeline_(software)
[16]
Wikpedia: The Free Encyclopedia. 2022. Blackboard (design pattern). Retrieved from https://en.wikipedia.org/wiki/Blackboard_(design_pattern)
[17]
Wikpedia: The Free Encyclopedia. 2022. Adapter pattern. Retrieved from https://en.wikipedia.org/wiki/Adapter_pattern
[18]
Wikpedia: The Free Encyclopedia. 2022. Iterator pattern. Retrieved from https://en.wikipedia.org/wiki/Iterator_pattern
[19]
Wikpedia: The Free Encyclopedia. 2022. Strategy pattern. Retrieved from https://en.wikipedia.org/wiki/Strategy_pattern