Skip to content
GDP for Noobs
☰ All patterns
BehavioralBeginner

Strategy

Swap algorithms at runtime behind one interface.

01

Explain it like I'm new

Real-life analogy

Getting to the airport

Car, train or bike: the goal is the same and you pick the strategy based on time and budget. The trip planner doesn't care how each works.

😩 The problem

Shipping cost depends on the method chosen. A growing switch statement in Cart mixes cart logic with every pricing rule.

💡 The pattern

Put each algorithm behind a common interface. Cart holds a strategy and calls it, and you can swap strategies at runtime.

02

Play with it

An interactive model of the pattern. Click around, nothing can break.

// swap the algorithm at runtime
cart.SetShipping(Standard{})
cart.ShippingCost() // → "$6.50"

💡 The Cart code never changes. You only swap which strategy it holds.

03

Walk through the code

Click a step to highlight its lines. This is a complete program you can copy and run.

main.go
1package main
2
3import "fmt"
4
5// ShippingStrategy is a family of interchangeable algorithms.
6type ShippingStrategy interface {
7 Name() string
8 Cost(weightKg float64) float64
9}
10
11type Standard struct{}
12type Express struct{}
13type Drone struct{}
14
15func (Standard) Name() string { return "Standard" }
16func (Standard) Cost(w float64) float64 { return 5 + w*1.0 }
17func (Express) Name() string { return "Express" }
18func (Express) Cost(w float64) float64 { return 15 + w*2.5 }
19func (Drone) Name() string { return "Drone" }
20func (Drone) Cost(w float64) float64 {
21 if w > 2 {
22 return -1 // drones can't lift heavy stuff
23 }
24 return 25
25}
26
27// Cart doesn't know HOW shipping is calculated. It just holds a strategy.
28type Cart struct {
29 weightKg float64
30 shipping ShippingStrategy
31}
32
33func (c *Cart) SetShipping(s ShippingStrategy) { c.shipping = s }
34
35func (c *Cart) ShippingCost() string {
36 cost := c.shipping.Cost(c.weightKg)
37 if cost < 0 {
38 return "not available"
39 }
40 return fmt.Sprintf("$%.2f", cost)
41}
42
43func main() {
44 for _, weight := range []float64{1.5, 4} {
45 cart := &Cart{weightKg: weight}
46 fmt.Printf("Package of %.1f kg:\n", weight)
47 // Swap the algorithm at runtime without touching Cart.
48 for _, s := range []ShippingStrategy{Standard{}, Express{}, Drone{}} {
49 cart.SetShipping(s)
50 fmt.Printf(" %-8s -> %s\n", s.Name(), cart.ShippingCost())
51 }
52 }
53}
terminal
Press ▶ Run to execute this program.

This output was captured by running the program with Go 1.24. Copy it into your editor or the Go Playground and try changing things.

04

When should I use it?

✅ Reach for it when…

  • •Multiple interchangeable algorithms: pricing, compression, sorting, auth methods.

🚫 Skip it when…

  • •There's only one algorithm and no plan for more.

🐹 Go tip

For one-method strategies, a function type is often simpler: type CostFunc func(kg float64) float64.

🌍 You've already seen it in

sort.Slice(data, less) takes the comparison strategy as a function. hash.Hash implementations are interchangeable.

05

Quick check

One question. No pressure.

sort.Slice(people, func(i, j int) bool {...}): what's the strategy here?

Related: State, Factory, Command