Skip to content
aipatterns.dev

Guide

Why Learn Patterns

What knowing design patterns buys a developer or coding agent: shared vocabulary, faster code reading, better refactoring targets, and knowing when to stop.

The short answer

You will meet these patterns whether you learn them or not. Frameworks, standard libraries and existing codebases are full of them. Learning patterns is mostly about reading code faster and deciding with fewer mistakes, not about writing more abstractions.

Benefits

A shared vocabulary

"Wrap the payment client in an Adapter" says in six words what would otherwise take a paragraph: a new class, a target interface the rest of the app already uses, and translation of calls and errors. In code review, a commit message or an agent's plan, names compress design discussion.

Faster code reading

When you recognize a role, you can predict the rest of the structure. Spotting a class named *Handler with a next field tells you to look for a Chain of Responsibility and find where the chain is assembled. Spotting subscribe/unsubscribe tells you to look for an Observer and check for listener leaks.

Common places you will see patterns without being told:

You seeLikely pattern
Express or Koa middleware, servlet filtersChain of Responsibility
addEventListener, RxJS, Node EventEmitterObserver
Query builders, StringBuilder, test data buildersBuilder
Java I/O streams, HTTP client interceptorsDecorator
ORM lazy-loaded relations, JS ProxyProxy
Undo stacks in editorsCommand and Memento
Compiler AST passesVisitor

Better refactoring targets

Smells tell you something is wrong. Patterns tell you what "better" can look like. A growing type switch (Switch Statements) has a known destination in Strategy or State. Two incompatible classes doing the same job (Alternative Classes with Different Interfaces) may need an Adapter. Having a destination keeps refactoring from drifting.

Knowing when to stop

Each pattern on this site has a "Do not use it when" section. Learning a pattern properly includes learning its misfits. Developers who know only the solution side apply it everywhere. Developers who know the forces apply it where it pays.

Why it matters more for agents

An AI coding agent generates code quickly, so the cost of over-engineering is paid by the humans who maintain the result. Patterns help an agent in specific ways:

  • Recognize existing structure before editing. If a codebase already uses Strategy for pricing rules, add a new strategy; don't add a new if inside the context.
  • Communicate intent. A plan that says "introduce a Facade over the three reporting services" is quick for a human to approve or reject.
  • Avoid reinventing. Many requests ("let users undo", "notify subscribers", "support a new storage backend") map onto known structures with known pitfalls.
  • Resist pattern inflation. The checklists on each page exist to stop an agent from turning a 20-line function into five classes.

See Using this reference as an agent for the full workflow.

What not to expect

  • Patterns do not make a design correct. A wrong abstraction behind a well-named pattern is still wrong.
  • Patterns are not a checklist to maximize. Code with zero named patterns can be excellent.
  • Patterns are not language-neutral in cost. Some vanish in languages with closures, generics or pattern matching. See History of patterns.

How to learn them efficiently

  1. Read the intent and the "Do not use it when" section of every pattern first. That gives you recognition and restraint.
  2. Learn the frequently seen ones in depth: Strategy, Observer, Adapter, Decorator, Factory Method, Builder, Facade.
  3. Learn the confusable pairs side by side: Strategy vs State, Decorator vs Proxy, Adapter vs Facade, Factory Method vs Abstract Factory (Factory comparison).
  4. Practice by refactoring toward a pattern from a real smell, not by writing one from scratch.

See also

Original writing and examples. Topic coverage follows Refactoring.Guru, which explains this topic with its own illustrations.