What Are Design Patterns?
Definition
Design pattern
- In plain English
- A design pattern is a named, reusable solution shape for a problem that keeps coming up in code.
- Think of it like
- It is like a sewing pattern you adapt to each new piece of cloth rather than a finished shirt.
Why it exists
Ten teams describe the same design in ten different ways. You spend the meeting translating instead of deciding.
A shared pattern name shortens that discussion. Copying a pattern without its problem can still bury readable code under layers.
How it works
Creational, structural and behavioural
- In plain English
- Creational patterns are about creating values, structural patterns about fitting types together, and behavioural patterns about how types share work.
- Think of it like
- Like a kitchen's three jobs: buying ingredients, arranging stations and running the service.
| Pattern | Category | Problem | Go shape |
|---|---|---|---|
| Factory | Creational | Repeated creation decisions | NewX function returning a value or interface |
| Builder | Creational | Settings need combined validation | Methods collect settings; Build validates |
| Singleton | Creational | One expensive shared value | One shared value with protected setup |
| Adapter | Structural | Mismatched interfaces | Wrapper translates method calls |
| Decorator | Structural | Extra behaviour around a value | Wrapper keeps the same interface |
| Strategy | Behavioural | Interchangeable ways to calculate | Function type or small interface |
| Observer | Behavioural | Many reactions to one event | A function or small interface |
| State | Behavioural | Actions depend on lifecycle stage | State types or a transition table |
| Command | Behavioural | Actions need storage or undo | State types or a table of allowed moves |
Patterns branches to Creational, Structural and Behavioural, each linked to its named patterns.
Anti-pattern
- In plain English
- An anti-pattern is a common "solution" that looks helpful but causes more problems than it solves.
- Think of it like
- Like taping over a warning light instead of fixing the engine.
Examples
Name the notification shape
Ten services react when a robot finishes a pick. Observer names the shared design.
package notifications
type Hub interface {
Subscribe(func(string))
Publish(string)
}
Subscribe registers a function to call later. Publish announces an event to the registered functions.
The insight
A pattern name compresses a whole design into one word.
Name the pricing choice
Prices vary for weekdays, weekends and long-term hire. Strategy names a replaceable pricing rule.
package pricing
type PriceRule func(days int) int
func Total(days int, rule PriceRule) int { return rule(days) }
PriceRule names the required inputs and result. Total uses the chosen rule without choosing the plan itself.
The insight
Spotting the recurring problem is the real skill.
Trade-offs
Pros
- Shared vocabulary.
- Tested solution shapes.
- Shorter design discussions.
Cons
- Easy to over-apply.
- Textbook forms can be heavier than Go needs.
Use when
- You recognise a recurring problem.
- A name helps your team discuss a design.
Avoid when
- You want the design to sound professional.
- A plain function already solves the problem.
Quick check
Takeaways
- Patterns are named solution shapes.
- Three families: creational, structural, behavioural.
- Start from the problem, not the pattern.