🏠 VisualStudioTutor.com  ·  C# Tutorial Home  ·  C# Lesson 26 of 40
Lesson 26 of 40 Architecture Advanced ⏱ 35 min

Design Patterns in C#

Use design patterns to name and solve recurring design problems. This lesson combines Strategy and Factory in a payment-processing example.

Part 1: What You Will Learn

Use design patterns to name and solve recurring design problems. This lesson combines Strategy and Factory in a payment-processing example.

  • Use Strategy to encapsulate interchangeable payment behavior.
  • Use Factory to centralize the creation of strategy objects.
  • Keep the checkout workflow independent of payment details.
  • Recognize when patterns simplify change—and when a plain class is enough.

Project setup: Create a .NET 10 Console App and add the interface and classes below.

Part 2: Topic-Specific Working Example

The following example is written specifically for this lesson. Create the project described above, enter the code, run it, and then change some values to observe how the feature behaves.

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

public sealed class CardPayment : IPaymentStrategy
{
    public void Pay(decimal amount) =>
        Console.WriteLine($"Paid {amount:C} by card.");
}

public sealed class BankTransferPayment : IPaymentStrategy
{
    public void Pay(decimal amount) =>
        Console.WriteLine($"Paid {amount:C} by bank transfer.");
}

public static class PaymentFactory
{
    public static IPaymentStrategy Create(string method) =>
        method.ToLowerInvariant() switch
        {
            "card" => new CardPayment(),
            "bank" => new BankTransferPayment(),
            _ => throw new ArgumentException("Unsupported payment method.")
        };
}

public sealed class Checkout(IPaymentStrategy payment)
{
    public void Complete(decimal total) => payment.Pay(total);
}

IPaymentStrategy strategy = PaymentFactory.Create("card");
Checkout checkout = new(strategy);
checkout.Complete(249.90m);

Part 3: How the Code Works

  • Strategy is represented by `IPaymentStrategy` and its interchangeable implementations.
  • The Factory decides which concrete strategy to create from the input.
  • `Checkout` remains focused on the workflow and does not contain card- or bank-specific logic.
  • Other useful patterns include Builder for staged object creation, Observer for notifications, Decorator for layered behavior, and Command for encapsulated actions.

Part 4: Mini Project & Practice

Mini project: add an EWalletPayment strategy. Then extend the example with a receipt decorator that logs each successful payment.

Tip: Type the code yourself in Visual Studio 2026, run it, then deliberately change one part at a time. The goal is to understand the feature rather than simply copy the finished example.

When you are comfortable with this lesson, continue to Lesson 27.

C# in Visual Studio 2026

📘 This lesson is part of the book C# in Visual Studio 2026 by Dr. Liew Voon Kiong.

View on Amazon Kindle Edition