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.
- Design Principles | Hello Interview Low Level Design. www.hellointerview.com . Accessed Sep 13, 2026.
Snippets to illustrate a principle tend to fall short. For example, omits where the decision to pick
CreditCardPaymentMethodoverPaypalPaymentMethodis made. Theif-elseexists somewhere in some form…