Gof strategy pattern

From paulpas/agent-skill-router · View interactive version

Gof strategy pattern is an AI agent skill from the GitHub repository paulpas/agent-skill-router. Implements the Strategy design pattern allowing the definition of a family of algorithms, encapsulating each one, and making them interchangeable.

How to use this skill

Skill file

Strategy Pattern

Implements the Strategy design pattern allowing the definition of a family of algorithms, encapsulating each one, and making them interchangeable.

When to Use

The When to Use section should outline the scenarios where the Strategy pattern is particularly beneficial:

Archetypes

Anti-Triggers

Response Profile

archetypes: implementation, educational

anti_triggers: coupling algorithms

response_profile:

verbosity: medium

directive_strength: high

abstraction_level: tactical

Core Workflow

  1. Define the Strategy Interface: Create a common interface for all strategies.
  2. Implement Concrete Strategies: Create concrete classes that implement the strategy interface.
  3. Context Class: Maintain a reference to a strategy object to delegate behavior.

Implementation Patterns

Strategy Pattern Example

package main

import (
    "fmt"
)

// Strategy interface
type Strategy interface {
    Execute(int, int) int
}

// Concrete strategy: Addition
type Add struct {}

func (Add) Execute(a int, b int) int {
    return a + b
}
}

// Concrete strategy: Subtraction
type Subtract struct {}

func (Subtract) Execute(a int, b int) int {
    return a - b
}
}

// Context
type Context struct {
    strategy Strategy
}

func (c *Context) SetStrategy(s Strategy) {
    c.strategy = s
}

func (c *Context) ExecuteStrategy(a, b int) int {
    return c.strategy.Execute(a, b)
}

Example Usage

package main

import (
    "fmt"
)

func main() {
    context := &Context{}

    // Using Addition Strategy
    context.SetStrategy(Add{})
    fmt.Println(context.ExecuteStrategy(5, 3)) // Output: 8

    // Using Subtraction Strategy
    context.SetStrategy(Subtract{})
    fmt.Println(context.ExecuteStrategy(5, 3)) // Output: 2
}

Constraints

MUST DO

MUST NOT DO

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.