C# Strategy Pattern Implementation Guide

C# Strategy Pattern Implementation Guide


Using various design patterns in software development makes the code more modular and easier to maintain. The Strategy Pattern is one of these patterns. In this article, we will explain what the Strategy Pattern is in C#, how to use it, and provide a sample implementation to help you understand it better.

What is the Strategy Pattern?

The Strategy Pattern allows an algorithm to be defined as an object and this object to be changed later as desired. This design pattern is useful when you want to use more than one algorithm for a particular process. For example, in a payment system, we can define different payment methods (credit card, PayPal, bank transfer) as strategies. In this way, when the application asks the user to choose the payment type, it can perform the transaction using the appropriate strategy.

Components of the Strategy Pattern

1. Context

The Context is the main object that allows the selection of algorithms. It contains a Strategy object and performs the desired operation using this object.

2. Strategy Interface

By creating an interface that provides the implementation of algorithms, we define different algorithm classes through this interface.

3. Concrete Strategies

These are concrete classes that implement the strategy interface. Each class provides a different algorithm.

Implementing the Strategy Pattern in C#

In the code example below, you will see the implementation of the strategy pattern for a payment process. Let's first define the necessary classes:

using System;

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

// Concrete Strategies
public class CreditCardPayment : IPaymentStrategy
{
    public void Pay(decimal amount)
    {
        Console.WriteLine($"{amount} TL paid by credit card.");
    }
}

public class PayPalPayment : IPaymentStrategy
{
    public void Pay(decimal amount)
    {
        Console.WriteLine($"{amount} TL paid via PayPal.");
    }
}

// Context
public class ShoppingCart
{
    private IPaymentStrategy _paymentStrategy;

    public void SetPaymentStrategy(IPaymentStrategy paymentStrategy)
    {
        _paymentStrategy = paymentStrategy;
    }

    public void Checkout(decimal amount)
    {
        _paymentStrategy.Pay(amount);
    }
}

With this implementation, we can easily add different payment methods. For example, we can use it as follows:

class Program
{
    static void Main(string[] args)
    {
        ShoppingCart cart = new ShoppingCart();

        // Payment by credit card
        cart.SetPaymentStrategy(new CreditCardPayment());
        cart.Checkout(150);

        // Payment via PayPal
        cart.SetPaymentStrategy(new PayPalPayment());
        cart.Checkout(200);
    }
}

In the above example, the user first selects the credit card as the payment method, then proceeds to payment via PayPal. Thanks to the Strategy Pattern, adding a new payment method to our application is quite easy; it is enough to create a new class and implement the IPaymentStrategy interface.

Conclusion

The Strategy Pattern makes the software development process more flexible and manageable in object-oriented languages like C#. By using the strategy pattern within software architecture, we can manage variable behaviors as we wish. In this article, we learned what the Strategy Pattern is, its components, and how to implement it with C#. Now you can easily use this pattern in your own projects.