System Design Academy

Saving…

Lesson 7 of 11 in Module 30 of 45 lessons complete

Strategy

Module 3, lesson 7Concept12 min

Definition

Strategy

In plain English
Strategy puts each version of an algorithm behind one interface so the caller can swap which one it uses.
Think of it like
It is like a sat-nav letting you choose fastest, shortest or no-motorways route.

Why it exists

Weekday, weekend and long-term rules repeat across billing. Every new plan adds another chain of conditions.

Choose a pricing rule once, then pass it to booking code. That code can use any rule with the same contract.

How it works

Function type

In plain English
A function type names a function signature, so any function with that shape can be passed around as a value.
Think of it like
Like a job title that anyone with the right skills can fill.
Interface-based strategies share the Price method.
Go
package main

import "fmt"

type Pricer interface{ Price(int) int }
type Weekday struct{}

func (Weekday) Price(days int) int { return days * 4000 }

type Weekend struct{}

func (Weekend) Price(days int) int { return days * 5000 }

type LongTerm struct{}

func (LongTerm) Price(days int) int { return days * 3000 }

type Booking struct{ pricing Pricer }

func (b Booking) Total(days int) int { return b.pricing.Price(days) }
func main()                          { fmt.Println((Booking{Weekend{}}).Total(2)) }
The same pricing choices can use a function type.
Go
package main

import "fmt"

type PriceFunc func(int) int

func total(days int, price PriceFunc) int { return price(days) }
func main() {
	rules := map[string]PriceFunc{
		"weekday":   func(days int) int { return days * 4000 },
		"weekend":   func(days int) int { return days * 5000 },
		"long-term": func(days int) int { return days * 3000 },
	}
	rule, ok := rules["weekend"]
	if !ok {
		fmt.Println("unknown plan")
		return
	}
	fmt.Println(total(2, rule))
}

Weekday, Weekend and LongTerm satisfy Pricer; Booking uses Pricer without choosing the implementation.

Booking delegates pricing to the selected strategy.

Weekday, Weekend and LongTerm satisfy Pricer; Booking uses Pricer without choosing the implementation.

The caller chooses the strategy. Check configuration names before calling a function selected from a map.

Examples

Select a pricing plan

Your booking chooses weekday, weekend or long-term pricing. Totals use whole days and integer cents.

Select a pricing plan.
Go
package main

import "fmt"

type PriceFunc func(int) int

func total(days int, price PriceFunc) int { return price(days) }
func main() {
	rules := map[string]PriceFunc{
		"weekday":   func(days int) int { return days * 4000 },
		"weekend":   func(days int) int { return days * 5000 },
		"long-term": func(days int) int { return days * 3000 },
	}
	rule, ok := rules["weekend"]
	if !ok {
		fmt.Println("unknown plan")
		return
	}
	fmt.Println(total(2, rule))
}

These sample rates demonstrate interchangeable rules. A real booking must also reject negative days and apply its own pricing terms.

The insight

Booking code stays the same while pricing varies.

Trade-offs

Pros

  • Swap algorithms independently.
  • Test each rule alone.
  • Keep callers free of condition chains.

Cons

  • The caller must choose.
  • Many tiny types can add noise.

Use when

  • Several ways perform the same task.
  • Rules change independently of callers.

Avoid when

  • Only one rule exists.
  • A stable two-branch decision already reads well.

Quick check

Pricing varies by weekday, weekend and long-term, and new schemes keep coming. Which pattern?
In Go, a strategy with one method is often written as…

Takeaways

  • Strategy = interchangeable algorithms behind one interface.
  • The caller picks; the code using it doesn't change.
  • In Go, a function type often suffices.

Try the quick check above whenever you feel ready.

Previous: Decorator