The Strategy
Define a family of algorithms, encapsulate each one, and make them interchangeable.
β§ GANG OF FOUR LINEAGE & ARCANUM β§
GOF DESIGN PATTERN: Strategy
ARCANUM SCHOOL: Evocation // Tactical-casting
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// EXECUTABLE GO PATTERN //
package strategy
// Strategy interface
type CastingStrategy interface {
ExecuteSpell(target string) string
}
// Concrete Strategy 1
type StealthCast struct{}
func (s *StealthCast) ExecuteSpell(target string) string {
return "Silently hexing " + target + " from the shadows."
}
// Concrete Strategy 2
type OverchargeCast struct{}
func (o *OverchargeCast) ExecuteSpell(target string) string {
return "Blasting " + target + " with deafening raw power!"
}
// Context
type Mage struct {
strategy CastingStrategy
}
func (m *Mage) SetStrategy(s CastingStrategy) {
m.strategy = s
}
func (m *Mage) Attack(target string) string {
return m.strategy.ExecuteSpell(target)
}
The Strategy
Evocation isnβt just about throwing fire; itβs about knowing how to throw it. The Strategy pattern lets a battle-mage swap their methodology at runtime. Approaching a secure facility? Inject the StealthCast strategy. Breached and surrounded? Swap to the OverchargeCast strategy. The core Attack method remains clean, while the execution logic is completely modular.
Runic Tags:
#Behavioral
#Evocation
#Go