$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
ArchitectureintermediateNew

Strategy Pattern

Share

Implement Strategy pattern

Works with OpenClaude

You are a software architect implementing the Strategy design pattern. The user wants to create interchangeable algorithms that can be selected at runtime without modifying client code.

What to check first

  • Identify the varying behavior that should be encapsulated (e.g., different sorting algorithms, payment methods, or filtering logic)
  • Verify that you have multiple concrete implementations of the same operation
  • Confirm the context object that will use these strategies needs to switch between them dynamically

Steps

  1. Define a Strategy interface with a single method representing the algorithm contract (e.g., execute(), calculate(), process())
  2. Create concrete strategy classes that implement this interface, each with a different algorithm
  3. Create a Context class that holds a reference to a Strategy object (use composition, not inheritance)
  4. Add a setter method in Context to swap strategies at runtime (e.g., setStrategy(Strategy strategy))
  5. In Context's main method, delegate the operation to the current strategy using the interface method
  6. Instantiate different strategies and pass them to Context to demonstrate runtime switching
  7. Verify each strategy executes independently without affecting other strategies
  8. Test that changing strategies mid-execution produces the expected behavioral change

Code

// Strategy Interface
interface PaymentStrategy {
    void pay(double amount);
}

// Concrete Strategies
class CreditCardPayment implements PaymentStrategy {
    private String cardNumber;
    private String cardHolder;
    
    public CreditCardPayment(String cardNumber, String cardHolder) {
        this.cardNumber = cardNumber;
        this.cardHolder = cardHolder;
    }
    
    @Override
    public void pay(double amount) {
        System.out.println("Paying $" + amount + " using Credit Card: " + cardNumber);
    }
}

class PayPalPayment implements PaymentStrategy {
    private String email;
    
    public PayPalPayment(String email) {
        this.email = email;
    }
    
    @Override
    public void pay(double amount) {
        System.out.println("Paying $" + amount + " using PayPal: " + email);
    }
}

class CryptoCurrencyPayment implements PaymentStrategy {
    private String walletAddress;
    
    public CryptoCurrencyPayment(String walletAddress) {
        this.walletAddress = walletAddress;
    }
    
    @Override
    public void pay(double amount) {
        System.out.println("Paying $" + amount + " using Cryptocurrency: " + walletAddress);
    }
}

// Context Class
class ShoppingCart {
    private PaymentStrategy paymentStrategy;
    private double totalAmount;
    
    public ShoppingCart(double totalAmount) {
        this.totalAmount = totalAmount;
    }
    
    public void setPaymentStrategy(PaymentStrategy strategy) {
        this.paymentStrategy = strategy;
    }
    
    public

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryArchitecture
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
architecturestrategypattern

Install command:

curl -o ~/.claude/skills/strategy-pattern.md https://claude-skills-hub.vercel.app/skills/architecture/strategy-pattern.md

Related Architecture Skills

Other Claude Code skills in the same category — free to download.

Want a Architecture skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.