Chapter 5. Writing Design Patterns

Although this book is aimed at those new to design patterns, a fundamental understanding of how a design pattern is written can offer a number of useful benefits. For starters, we can gain a deeper appreciation for the reasoning behind why a pattern is needed. We can also learn how to tell if a pattern (or proto-pattern) is up to scratch when reviewing it for our own needs.

Writing good patterns is a challenging task. Patterns not only need to (ideally) provide a substantial quantity of reference material for end users, but they also need to be able to defend why they are necessary.

Having read the previous section on what a pattern is, we may think that this in itself is enough to help us identify patterns we see in the wild. This is actually not completely true. It’s not always clear if a piece of code we’re looking at is following a set pattern or just accidentally happens to appear like it does.

When we’re looking at a body of code we think may be using a pattern, we should consider writing down some of the aspects of the code that we believe fall under a particular existing pattern or set of patterns.

In many cases of pattern analysis, we can find that we’re just looking at code that follows good principles and design practices that could happen to overlap with the rules for a pattern by accident. Remember: solutions in which neither interactions nor defined rules appear are not patterns.

If you are interested in venturing down the path of writing your own design patterns, I recommend learning from others who have already been through the process and done it well. Spend time absorbing the information from a number of different design pattern descriptions and take in what’s meaningful to you.

Explore structure and semantics—this can be done by examining the interactions and context of the patterns you are interested in so you can identify the principles that assist in organizing those patterns together in useful configurations.

Once we’ve exposed ourselves to a wealth of information on pattern literature, we may wish to begin writing our pattern using an existing format and see if we can brainstorm new ideas for improving it or integrating our ideas in there.

An example of a developer that did this in recent years is Christian Heilmann, who took the existing Module pattern and made some fundamentally useful changes to it to create the Revealing Module pattern (this is one of the patterns covered later in this book).

The following are tips I would suggest if you are interested in creating a new design pattern:

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.

The design decisions we make should be based on principles we derive from an understanding of best practices.

Our design patterns should be transparent to the user.

Design patterns should be entirely transparent to any type of user experience. They are primarily there to serve the developers using them and should not force changes to behavior in the user experience that would not be incurred without the use of a pattern.

Remember that originality is not key in pattern design.

When writing a pattern, you do not need to be the original discoverer of the solutions being documented nor do you have to worry about your design overlapping with minor pieces of other patterns. If the approach is strong enough to have broad useful applicability, 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 strong set of examples demonstrating the successful application of the pattern. To show broad usage, examples that exhibit good 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 when writing a pattern you cover the widest possible areas of application. I hope that this brief introduction to writing patterns has given you some insights that will assist your learning process for the next sections of this book.

Get Learning JavaScript Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.