Strategy Pattern

It lets me define a family of algorithms, encapsulate each one, and make them interchangeable.

Strategy lets the algorithm vary independently from clients that use it.

✅ When to Use Strategy Pattern

🏗️ Structure of Strategy Pattern

type Strategy interface {
    Execute(data string) string
}
type ConcreteStrategyA struct{}
func (s *ConcreteStrategyA) Execute(data string) string {
    return "ConcreteStrategyA: " + data
}
type ConcreteStrategyB struct{}
func (s *ConcreteStrategyB) Execute(data string) string {
    return "ConcreteStrategyB: " + data
}
type Context struct {
    strategy Strategy
}
func (c *Context) SetStrategy(strategy Strategy) {
    c.strategy = strategy
}
func (c *Context) ExecuteStrategy(data string) string {
    return c.strategy.Execute(data)
}