A practical catalog of the 23 classic design patterns, with small Go implementations, behavior tests and runnable examples. Maintained by alifarid.
The examples keep the original chapter layout. New packages fill the missing patterns; existing examples show both the abstraction and its failure cases. Everything uses the standard library. These are learning examples, with ownership and concurrency limits explained in the code and pattern guide.
Install Go 1.22.5 or newer; a current stable Go release is recommended.
git clone https://github.com/Alifarid0011/design_patterns.git
cd design_patterns
go test ./...
go test -run Example -v ./...Tests run without external services or API keys. Run one package while studying it:
go test -v ./chapter5/behavioralPatterns/observer
go doc ./chapter5/behavioralPatterns/observerRead the learning path for an order that builds on Go's interfaces, composition and functions. The pattern guide explains when each pattern helps and when a simpler Go construct is enough.
| Pattern | Example | What it demonstrates |
|---|---|---|
| Factory Method | factorymethod | A workflow delegates product creation |
| Abstract Factory | abstractFactory | Related products from a compatible family |
| Builder | builder.go | One construction sequence, different products |
| Prototype | prototype.go | Independent copies of a preset |
| Singleton | singleton.go | Shared identity and synchronized updates |
The original payment example is a simple factory, useful in its own right. It is distinct from Factory Method.
| Pattern | Example | What it demonstrates |
|---|---|---|
| Adapter | Adapter.go | Fit an existing object to a different interface |
| Bridge | Bridge.go | Vary a printer and its output backend independently |
| Composite | composite | Treat leaves and nested groups uniformly |
| Decorator | decorator.go | Add behavior through wrappers |
| Facade | Facade.go | Hide HTTP and decoding behind a weather query |
| Flyweight | Flyweight.go | Reuse shared team data |
| Proxy | Proxy.go | Cache access to a backing user list |
Struct embedding and composition are separate Go language lessons; embedding alone is not the Composite pattern.
| Pattern | Example | What it demonstrates |
|---|---|---|
| Chain of Responsibility | chainOfResponsibility.go | Forward a request through handlers |
| Command | command | Execute and undo operations |
| Interpreter | interpreter | Evaluate a small expression language |
| Iterator | iterator | Traverse a collection without exposing its position |
| Mediator | mediator | Route communication through a coordinator |
| Memento | memento | Capture and restore an opaque snapshot |
| Observer | observer | Subscribe to synchronous events |
| State | state | Enforce lifecycle transitions |
| Strategy | strategy | Swap output algorithms behind an interface |
| Template Method | templatemethod | Keep a workflow fixed while replacing its steps |
| Visitor | visitor | Apply operations across different node types |
go run ./chapter1/test 12 30
go run ./chapter5/behavioralPatterns/strategy/cli -output text
go run ./chapter5/behavioralPatterns/strategy/cli -output image -output-path square.jpgSeveral original chapter directories use package main as teaching notebooks.
Use their tests to explore each example; the new focused packages can also be
imported from other examples in this module.
go vet ./...
go build ./...
go test -count=3 -shuffle=on ./...
go test -race ./...The race detector requires a supported platform and a C compiler. Shared global state is not a reason to use Singleton by default: prefer explicit dependencies when they make ownership and testing clearer. Most example objects are intended for sequential use unless their documentation explicitly promises synchronization.
See CONTRIBUTING.md for the expected scope and checks.