Open/Closed Principle

Dated Sep 13, 2026; last modified on Sun, 13 Sep 2026

Classes should be open for extension but closed for modification. Design with interfaces so that adding new functionality is a matter of writing new classes that implement those interfaces. Old code never changes, so it can’t break.

For example, instead of:

public class PaymentProcessor
{
  public void Process(string type, double amount)
  {
    if (type == "creditCard")
    {
      // Credit card logic
    }
    else if (type == "paypal")
    {
      // PayPal logic
    }

    // Adding crypto means modifying this method.
  }
}

… have:

public interface IPaymentMethod
{
  void Process(double amount);
}

public class CreditCardPaymentMethod : IPaymentMethod
{
  public void Process(double amount) { ... }
}

public class PaypalPaymentMethod : IPaymentMethod
{
  public void Process(double amount) { ... }
}

public class PaymentProcessor
{
  public void Process(IPaymentMethod paymentMethod, double amount) => paymentMethod.Process(amount);
}

… so that adding crypto payments amounts to creating a CryptoPaymentMethod class extending IPaymentMethod without changing PaymentProcessor.

Snippets to illustrate a principle tend to fall short. For example, omits where the decision to pick CreditCardPaymentMethod over PaypalPaymentMethod is made. The if-else exists somewhere in some form…

  1. Design Principles | Hello Interview Low Level Design. www.hellointerview.com . Accessed Sep 13, 2026.