System Design Academy

Saving…

Lesson 11 of 11 in Module 30 of 45 lessons complete

Choosing a Pattern

Module 3, lesson 11Concept12 min

Definition

Pattern smell

In plain English
A pattern smell is code using a pattern where a simpler solution would do.
Think of it like
It is like using a forklift to move a chair.

Why it exists

A team wraps every three-field value in factories and builders. Reading one reminder now means following four layers. This is a pattern smell.

Patterns with similar diagrams can solve different problems. Decide from the intended change, then consider whether a smaller design is enough.

How it works

Look-alikesFirst intentSecond intent
Strategy / StateCaller chooses the calculationCurrent state controls rules and transitions
Decorator / AdapterSame interface, extra behaviourTranslate into a different interface
Factory / BuilderChoose type or centralize setupAssemble and validate one value
Observer / CommandAnnounce what happenedRepresent work you want done

The root branches into creation, type fit and work; each branch names the patterns that address those needs.

Start with the problem, then narrow the design.

The root branches into creation, type fit and work; each branch names the patterns that address those needs.

  1. Name the pain

    Describe the change that keeps forcing edits or mistakes.

  2. Try the smallest shape

    Start with a direct function, literal or switch.

  3. Compare intent

    Use the table to choose a pattern only if it addresses that pain.

  4. Check the cost

    Keep the new design only if its benefit outweighs the extra layers.

Examples

Compare who chooses

The operator chooses the pricing plan. The charger changes its own state when charging fails.

Complete library with Demo: compare who chooses.
Go
package comparison

import "fmt"

type PriceRule func(int) int
type State interface{ Tick(*Charger, bool) }
type Charger struct{ state State }
type Available struct{}

func (Available) Tick(c *Charger, failed bool) {
	if failed {
		c.state = Faulted{}
		return
	}
	c.state = Charging{}
}

type Charging struct{}

func (Charging) Tick(c *Charger, failed bool) {
	if failed {
		c.state = Faulted{}
	}
}

type Faulted struct{}

func (Faulted) Tick(c *Charger, failed bool) {}
func Demo() {
	plan := PriceRule(func(kwh int) int { return kwh * 30 })
	c := &Charger{state: Available{}}
	c.state.Tick(c, false)
	c.state.Tick(c, true)
	fmt.Printf("price %d, state %T\n", plan(10), c.state)
}

PriceRule comes from the caller. Tick changes the charger’s state internally; this small demo treats the first healthy tick as starting charging.

The insight

Who decides the switch helps distinguish Strategy from State.

Trade-offs

Pros

  • Shared vocabulary.
  • Proven shapes for recurring problems.

Cons

  • Unneeded layers slow reading.
  • Similar shapes can obscure different intentions.

Use when

  • The recurring problem matches the pattern.
  • The design makes a likely change easier.

Avoid when

  • A plain function already reads well.
  • A struct or switch covers the whole need.

Quick check

The charger switches itself from Charging to Faulted when it detects an error. Strategy or State?
You wrap a client to add logging, keeping its interface unchanged. Adapter or Decorator?

Takeaways

  • Tell look-alikes apart by intent, not diagram shape.
  • Strategy: caller chooses; State: object changes itself.
  • Often the best pattern is none.

Try the quick check above whenever you feel ready.

Previous: Command