Design Patterns Mastery

Strategy Pattern: Swappable algorithms at runtime

1 Views Updated 5/4/2026

The Strategy Pattern

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from the clients that use it. This is the #1 tool for eliminating 100-line if/else blocks used for selecting logic.

1. Scenario: Payment Selection

A user can pay via CreditCard, PayPal, or Bitcoin. Instead of one giant Process() method, you create three Strategy classes.

public interface IPaymentStrategy 
{
    void Pay(decimal amount);
}

// THE CLIENT (Context)
public class CheckoutProcess 
{
    private IPaymentStrategy _strategy;
    
    public void SetStrategy(IPaymentStrategy s) => _strategy = s;

    public void Checkout(decimal amount) => _strategy.Pay(amount);
}

2. Why use it?

It follows the Open/Closed Principle. Next year, when the marketing team wants to add "Pay with Reward Points," you don't edit your existing checkout logic. You just create a new RewardPointStrategy class. The core system remains stable and untouched.

4. Interview Mastery

Q: "How is the Strategy pattern different from Dependency Injection?"

Architect Answer: "They are highly complementary. **Dependency Injection** is the *Mechanism* we use to get the strategy into the class. The **Strategy Pattern** is the *Architectural Design* of having multiple swappable implementations of an interface to handle different business rules. Strategy defines the 'Shape' of the solution; DI is the 'Plumbing' that connects it."

Design Patterns Mastery
1. Introduction to Design Patterns
Introduction to GoF Patterns: Why patterns matter? SOLID Principles: The foundation of all patterns DRY, KISS, and YAGNI (Architectural Philosophy)
2. Creational Patterns
Singleton Pattern: Thread-safety & Captive Dependencies Factory Method: Abstracting complex object creation Abstract Factory: Creating families of related objects Builder Pattern: Constructing complex fluents Prototype Pattern: Cloning high-cost objects
3. Structural Patterns
Adapter Pattern: Bridging incompatible interfaces Bridge Pattern: Decoupling abstraction from implementation Composite Pattern: Managing tree structures & hierarchies Decorator Pattern: Enhancing behavior without inheritance Facade Pattern: Simplifying complex library subsystems Flyweight Pattern: Drastically reducing memory footprint Proxy Pattern: Interception, Lazy-Loading, and Security
4. Behavioral Patterns
Chain of Responsibility: Middleware & Pipeline Architecture Command Pattern: Implementing Undo/Redo & Queueing Interpreter Pattern: Building domain-specific languages Iterator Pattern: Unified traversal of collections Mediator Pattern: Decoupling components with MediatR Memento Pattern: Capturing and restoring object state Observer Pattern: Pub/Sub & Event-driven architecture State Pattern: Managing complex object lifecycles Strategy Pattern: Swappable algorithms at runtime Template Method: Defining skeleton algorithms Visitor Pattern: Separating operations from data structures
5. Modern Enterprise & Cloud Patterns
Repository & Unit of Work (The EF Core Standard) CQRS Pattern (Command Query Responsibility Segregation) Circuit Breaker & Retry Patterns (Resilience with Polly) Dependency Injection Pattern (The Modern King)