Install
openclaw skills install bookforge-behavioral-pattern-selectorChoose the right behavioral design pattern from the 11 GoF behavioral patterns. Use when someone asks "how do I make this algorithm swappable?", "should I us...
openclaw skills install bookforge-behavioral-pattern-selectorYou have identified that a problem is behavioral — it concerns how objects communicate, distribute responsibilities, or vary their actions — and need to pick the right one of the 11 GoF behavioral patterns. This skill is appropriate when:
Before starting, confirm:
design-pattern-selector first for purpose classification.SUFFICIENT: problem description + what varies identified
PROCEED WITH DEFAULTS: rough problem description only (skill will help classify)
MUST ASK: no behavioral problem articulated at all
ACTION: Use TodoWrite to track progress, then capture the behavioral problem in structured form.
WHY: Behavioral patterns share surface similarities — Strategy, State, and Template Method all vary an algorithm, but for fundamentally different reasons. Without explicitly articulating what varies and why, you risk picking a pattern that fits the shape of the problem but not the intent. Forcing structured framing surfaces the distinction.
TodoWrite:
- [ ] Step 1: Characterize the behavioral problem
- [ ] Step 2: Apply Taxonomy 1 — What aspect to encapsulate
- [ ] Step 3: Apply Taxonomy 2 — Sender-receiver decoupling (if applicable)
- [ ] Step 4: Resolve ambiguous pairs with differentiating questions
- [ ] Step 5: Produce recommendation with trade-offs
Capture these elements from the user's description:
| Element | Question to answer |
|---|---|
| What varies | What behavior or aspect is expected to change across contexts or time? |
| Who decides | Does the client choose, or does the object itself decide based on state? |
| Coupling concern | Must the sender of a request be decoupled from who handles it? |
| Stability | Is the object structure stable but operations on it need to grow? |
| Lifecycle | Does the varying thing need to exist independently (be passed around, stored, undone)? |
If more than one element is unclear, ask one targeted question and wait for a response before continuing.
ACTION: Match the problem against the encapsulation taxonomy. Identify which aspect of the program is expected to change and therefore needs its own object.
WHY: The defining theme of behavioral patterns is encapsulating variation. Each pattern names the thing that changes and lifts it into a dedicated object. If you can identify what that thing is, you can identify the pattern — because the pattern is literally named for it. Skipping this step leads to pattern matching by feel rather than by structure.
Encapsulation taxonomy:
| Encapsulated aspect | Pattern | The object holds... |
|---|---|---|
| An algorithm | Strategy | The interchangeable computation — clients choose which strategy to install |
| State-dependent behavior | State | Behavior for one state of a context — object self-transitions at runtime |
| Inter-object protocol | Mediator | The communication rules between a group of colleague objects |
| Traversal method | Iterator | The logic for stepping through an aggregate's elements without exposing structure |
| A request (invocation) | Command | One operation — parameterized, deferrable, undoable, loggable |
| A state snapshot | Memento | A private checkpoint of an object's internals, opaque to all but the originator |
| Operations on a structure | Visitor | A family of operations across an object structure — defined outside the classes |
Three patterns that do not fit this taxonomy (they work at the class or communication level):
| Pattern | What it does instead |
|---|---|
| Template Method | Uses inheritance, not encapsulation in a separate object — a superclass skeleton calls abstract steps that subclasses fill in |
| Observer | Encapsulates a dependency relationship, not a discrete aspect — subjects and observers cooperate to maintain a constraint |
| Chain of Responsibility | Distributes handling across an open-ended chain — no single encapsulating object |
| Interpreter | Represents a grammar as a class hierarchy — each grammar rule is a class, not a standalone encapsulated object |
Output: Identify the encapsulated aspect and the candidate pattern(s) it points to. If the problem description mentions both an algorithm and state-driven behavior, or both communication and a protocol object, flag both candidates and proceed to Step 4.
ACTION: If the problem involves decoupling who sends a request from who handles it, evaluate the four decoupling patterns against each other.
WHY: Command, Observer, Mediator, and Chain of Responsibility all decouple senders from receivers, but with very different structures and trade-offs. Choosing the wrong one produces either the right decoupling for the wrong granularity (Observer when you need Command) or centralized logic that makes the system harder to understand (Mediator when Observer's distributed model is sufficient). This taxonomy makes the choice explicit.
Apply only if the problem includes any of these:
Decoupling comparison:
| Pattern | Coupling model | Cardinality | Receiver identity | Key trade-off |
|---|---|---|---|---|
| Command | Object binding — command holds reference to receiver | 1 sender : 1 receiver (per command) | Known at command-creation time; hidden from invoker | Invoker stays decoupled; a subclass is nominally required per sender-receiver pair |
| Observer | Interface-based — subject notifies any attached observer | 1 subject : many observers, dynamic | Not known to subject; observers self-register | Promotes loose coupling and fine-grained reuse; communication flow is harder to trace |
| Mediator | Centralized — all colleagues route through mediator | Many senders : many receivers via one hub | Hidden from colleagues; mediator knows all | Simplifies colleague interaction; mediator itself can become a complexity bottleneck |
| Chain of Responsibility | Chain forwarding — request walks a linked list of handlers | 1 sender : implicit receiver (first willing handler) | Not known to sender; determined at runtime by chain order | Very loose coupling; warning: request may go unhandled if no handler claims it |
Decision rules:
Output: If this taxonomy applies, add the appropriate decoupling pattern as a candidate or as a confirmation of the candidate from Taxonomy 1.
ACTION: If two or more candidates remain after Steps 2–3, apply the differentiating questions for the relevant pair.
WHY: Several behavioral patterns share structural similarities. Strategy and State have identical class diagrams. Template Method and Strategy both vary algorithms. Without asking the right differentiating question, you will correctly identify the problem category but pick the wrong pattern.
Pair: Strategy vs. State
Both encapsulate behavior that can be swapped at runtime via a context object. The difference is who initiates the swap and why:
| Question | Strategy → if... | State → if... |
|---|---|---|
| Who changes the behavior? | The client installs a strategy explicitly | The context object itself transitions to a new state based on internal logic |
| Do the concrete variants know about each other? | No — strategies are independent | Yes — state objects often initiate transitions to sibling states |
| Is the variation about selecting an algorithm? | Yes — same interface, different computation | No — about representing a lifecycle that drives behavior |
Pair: Template Method vs. Strategy
Both vary an algorithm, but via opposite mechanisms:
| Question | Template Method → if... | Strategy → if... |
|---|---|---|
| How is variation expressed? | Inheritance — a subclass overrides abstract steps | Composition — a context object holds a replaceable strategy |
| Can the algorithm be swapped at runtime? | No — fixed at class definition time | Yes — a different strategy object can be installed at any time |
| Do you control the superclass? | Yes — you define the skeleton | Doesn't matter — client provides the algorithm |
Pair: Observer vs. Mediator
Both decouple objects from direct references, but in opposite directions:
| Question | Observer → if... | Mediator → if... |
|---|---|---|
| Where does communication logic live? | Distributed — observers and subjects cooperate to maintain the constraint | Centralized — the mediator owns all routing logic |
| What is the coupling concern? | One object changes; unknown others must react | Many objects interact; the interactions themselves are complex |
| What is more important? | Reusability of subjects and observers | Understandability of the overall interaction flow |
Pair: Command vs. Chain of Responsibility
Both issue a request without the sender knowing the receiver, but via different mechanisms:
| Question | Command → if... | Chain of Responsibility → if... |
|---|---|---|
| Is there a known receiver at request-creation time? | Yes — command holds a reference to its receiver | No — the receiver is determined by walking the chain |
| Does the request need to be stored, undone, or replayed? | Yes — Command is a first-class object | No — the request is transient |
| Is exactly one handler expected? | Yes | Not necessarily — could be zero (silent drop) or one |
Output: A single confirmed pattern, or at most two if the problem genuinely requires both (e.g., Command + Chain of Responsibility is a valid composition — the chain uses Command objects as the request representation).
ACTION: Deliver the recommendation in the format below. Include the taxonomy reasoning that led to it, the key trade-off, and the runner-up pattern with why it was not chosen.
WHY: A recommendation without reasoning is a guess. The user needs to understand the taxonomy path that produced the choice — both to validate it and to apply the pattern correctly. The trade-off section prevents misapplication in edge cases.
Recommendation format:
## Pattern Recommendation: [Pattern Name]
**Category:** Behavioral — [Class / Object] scope
**Encapsulation taxonomy:** [What aspect is being encapsulated as an object]
**Decoupling taxonomy:** [If applicable — which sender-receiver model applies]
**Why this pattern:** [1–2 sentences connecting the pattern's intent to the specific problem]
**What it enables:** [What becomes possible that wasn't before]
**Key trade-off:** [What complexity or constraint is introduced]
**Warning (if applicable):** [Chain of Responsibility: silent drop risk; Mediator: bottleneck risk; etc.]
**Alternative considered:** [Runner-up pattern] — [Why it was not chosen]
**Patterns to combine with:** [Any complementary patterns]
Common combinations:
Scenario: An e-commerce system computes tax differently depending on region (US federal, EU VAT, Canadian GST). The tax logic is embedded in the Order class as a large if/else block. A new region is coming and the team does not want to keep modifying Order.
Trigger: "We have different tax rules per region and the order class keeps getting bigger every time we add one."
Process:
Output:
## Pattern Recommendation: Strategy
**Category:** Behavioral — Object scope
**Encapsulation taxonomy:** Algorithm (tax computation rule) is extracted into a TaxStrategy object
**Why this pattern:** Strategy replaces the if/else dispatch with a TaxStrategy
interface. Each region (USTaxStrategy, EUVATStrategy, CAGSTStrategy) is a
ConcreteStrategy. Order holds a reference and delegates to it.
**What it enables:** New regions are new TaxStrategy subclasses — Order is never
modified. Strategies can be tested independently.
**Key trade-off:** Clients must know which strategy to instantiate; this knowledge
must come from somewhere (configuration, factory). If strategy selection is itself
complex, pair with Abstract Factory.
**Alternative considered:** Template Method — would require subclassing Order for
each region (OrderForUS, OrderForEU), which tightly couples the algorithm variation
to the order lifecycle. Strategy via composition is cleaner.
**Patterns to combine with:** Abstract Factory (to instantiate the correct strategy
based on region configuration)
Scenario: A wizard dialog has three states: empty (all fields disabled except the first), partial (some fields enabled, Submit disabled), and complete (Submit enabled). Currently, every button-click handler contains the same conditional: "if state == X, enable Y, disable Z." The logic is duplicated across 12 event handlers.
Trigger: "Every event handler has to know what state the dialog is in. Adding a new state means touching all 12 handlers."
Process:
Output:
## Pattern Recommendation: State
**Category:** Behavioral — Object scope
**Encapsulation taxonomy:** State-dependent behavior — each wizard state (EmptyState,
PartialState, CompleteState) encapsulates its own enable/disable logic
**Why this pattern:** Each state object handles the event and applies the correct
enable/disable rules for that state. The wizard context delegates all event handling
to its current state object. Transitions (EmptyState → PartialState) are initiated by
the state objects themselves when conditions are met.
**What it enables:** Adding a new state is a new class — existing handlers are not
touched. The 12 duplicated conditionals collapse into the state objects that own them.
**Key trade-off:** State objects may need access to the context to initiate transitions,
introducing some coupling. The number of classes grows by one per state.
**Alternative considered:** Strategy — the behavior here is not client-selected; the
dialog self-transitions. Strategy would require the external caller to manage state
changes, which belongs inside the dialog lifecycle.
Scenario: A spreadsheet application must support unlimited undo/redo for cell edits, formula applications, and formatting changes. Operations may take several seconds. The team also wants a log of all executed operations for crash recovery.
Trigger: "We need full undo/redo and also want to replay operations from a log after a crash."
Process:
Output:
## Pattern Recommendation: Command
**Category:** Behavioral — Object scope
**Encapsulation taxonomy:** Request (each operation) is encapsulated as a Command
object with Execute() and Unexecute() operations
**Decoupling taxonomy:** 1 invoker (toolbar/keyboard) : 1 receiver (cell/formula/formatter)
per command — invoker stays decoupled from receiver details
**Why this pattern:** Each operation (EditCellCommand, ApplyFormulaCommand,
FormatRangeCommand) is an object. Execute() applies it; Unexecute() reverses it.
The history list holds executed commands. Undo traverses it backward; redo forward.
Crash recovery replays the log by re-executing stored commands.
**What it enables:** Unlimited undo/redo with no special logic in the UI. Crash
recovery by replaying the command log. Macro recording by composing commands into
MacroCommands.
**Key trade-off:** Each unique operation requires a Command subclass. If operations
are highly varied (many receiver types), this produces many classes. Closures or
lambdas can replace subclasses in languages that support first-class functions.
**Alternative considered:** Memento — captures state snapshots for undo but stores
full state copies, which is expensive for large spreadsheets. Command stores only the
delta (what changed), which is far more efficient.
**Patterns to combine with:** Memento (for operations whose reversal cannot be
computed — store a snapshot as part of the command's undo data)
| File | Contents |
|---|---|
references/behavioral-comparison.md | All 11 patterns: applicability summary, encapsulation type, decoupling role, key trade-offs, and confusable pairs |
This skill is licensed under CC-BY-SA-4.0. Source: BookForge — Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides.
Install related skills from ClawhHub:
clawhub install bookforge-design-pattern-selectorOr install the full book set from GitHub: bookforge-skills