Chapter 3. Structuring and Writing Patterns

The success of a new idea depends on its utility and also on how you present it to those it is trying to help. For developers to understand and adopt a design pattern, it should be presented with relevant information about the context, circumstances, prerequisites, and significant examples. This chapter applies to those trying to understand a specific pattern and those trying to introduce a new one because it provides essential information on how patterns are structured and written.

The Structure of a Design Pattern

Pattern authors would be unable to successfully create and publish a pattern if they cannot define its purpose. Similarly, developers will find understanding or implementing a pattern challenging if they do not have a background or context.

Pattern authors must outline a new pattern’s design, implementation, and purpose. Authors initially present a new pattern in the form of a rule that establishes a relationship between:

  • A context

  • A system of forces that arises in that context

  • A configuration that allows these forces to resolve themselves in context

With this in mind, let’s now summarize the component elements for a design pattern. A design pattern should have the following, with the first five elements being the most important:

Pattern name

A unique name representative of the purpose of the pattern.

Description

A brief description of what the pattern helps achieve.

Context outline

The contexts in which the pattern effectively responds to its users’ needs.

Problem statement

A statement of the problem addressed so that we understand the pattern’s intent.

Solution

A description of how the user’s problem is solved in an understandable list of steps and perceptions.

Design

A description of the pattern’s design and, in particular, the user’s behavior in interacting with it.

Implementation

A guide to how developers would implement the pattern.

Illustrations

Visual representations of classes in the pattern (e.g., a diagram).

Examples

Implementations of the pattern in a minimal form.

Corequisites

What other patterns may be needed to support the use of the pattern being described?

Relations

What patterns does this pattern resemble? Does it closely mimic any others?

Known usage

Is the pattern being used in the wild? If so, where and how?

Discussions

The team’s or author’s thoughts on the exciting benefits of the pattern.

Well-Written Patterns

Understanding the structure and purpose of a design pattern can help us gain a deeper appreciation for the reasoning behind why a pattern is needed. It also helps us evaluate the pattern for our own needs.

A good pattern should ideally provide a substantial quantity of reference material for end users. Patterns should also provide evidence of why they are necessary.

Just having an overview of a pattern is not enough to help us identify them in the code we may encounter day-to-day. It’s not always clear if a piece of code we’re looking at follows a set pattern or accidentally resembles one.

If you suspect that the code you see uses a pattern, consider writing down some aspects of the code that fall under a particular existing pattern or set of patterns. It may be that the code follows sound principles and design practices that coincidentally overlap with the rules for a specific pattern.

Tip

Solutions in which neither interactions nor defined rules appear are not patterns.

Although patterns may have a high initial cost in the planning and write-up phases, the value returned from that investment can be worth it. Patterns are valuable because they help to get all the developers in an organization or team on the same page when creating or maintaining solutions. If you’re considering working on a pattern of your own, research beforehand, as you may find it more beneficial to use or extend existing, proven patterns rather than starting afresh.

Writing a Pattern

If you’re trying to develop a design pattern yourself, I recommend learning from others who have already been through the process and done it well. Spend time absorbing the information from several different design pattern descriptions and take in what’s meaningful to you. Explore structure and semantics—you can do this by examining the interactions and context of the patterns you’re interested in to identify the principles that assist in organizing those patterns together in valuable configurations.

You can utilize the existing format to write your own pattern or see if there are ways to improve it by integrating your ideas. An example of a developer who did this in recent years is Christian Heilmann, who took the existing Module pattern (see Figure 7-2) and made some fundamentally valuable changes to it to create the Revealing Module pattern (see “The Revealing Module Pattern”).

Adhering to the following checklist would help if you’re interested in creating a new design pattern or adapting an existing one:

How practical is the pattern?

Ensure that the pattern describes proven solutions to recurring problems rather than just speculative solutions that haven’t been qualified.

Keep best practices in mind.

Our design decisions should be based on principles we derive from understanding best practices.

Our design patterns should be transparent to the user.

Design patterns should be entirely transparent to the end-user experience. They primarily serve the developers using them and should not force changes to the expected user experience.

Remember that originality is not key in pattern design.

When writing a pattern, you do not need to be the original discoverer of the documented solutions, nor do you have to worry about your design overlapping with minor pieces of other patterns. If the approach is strong enough to be broadly applicable, it has a chance of being recognized as a valid pattern.

Patterns need a strong set of examples.

A good pattern description needs to be followed by an equally effective set of examples demonstrating the successful application of the pattern. To show broad usage, examples that exhibit sound design principles are ideal.

Pattern writing is a careful balance between creating a design that is general, specific, and, above all, useful. Try to ensure that you comprehensively cover all possible application areas when writing a pattern.

Whether you write a pattern or not, I hope this brief introduction to writing patterns has given you some insights that will assist your learning process and help you rationalize the patterns covered in the following sections of this book.

Summary

This chapter painted a picture of the ideal “good” pattern. It is equally important to understand that “bad” patterns exist, too, so we can identify and avoid them. And that is why we have the next chapter about “anti-patterns.”