Implement Strategy pattern
✓Works with OpenClaudeYou 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
- Define a
Strategyinterface with a single method representing the algorithm contract (e.g.,execute(),calculate(),process()) - Create concrete strategy classes that implement this interface, each with a different algorithm
- Create a
Contextclass that holds a reference to a Strategy object (use composition, not inheritance) - Add a setter method in Context to swap strategies at runtime (e.g.,
setStrategy(Strategy strategy)) - In Context's main method, delegate the operation to the current strategy using the interface method
- Instantiate different strategies and pass them to Context to demonstrate runtime switching
- Verify each strategy executes independently without affecting other strategies
- 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
Related Architecture Skills
Other Claude Code skills in the same category — free to download.
Singleton Pattern
Implement Singleton pattern
Factory Pattern
Implement Factory pattern
Observer Pattern
Implement Observer/PubSub pattern
Repository Pattern
Implement Repository pattern for data access
Dependency Injection
Set up dependency injection
CQRS Setup
Implement CQRS pattern
Event Sourcing
Implement Event Sourcing pattern
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.