SOLID Principles: Open-Closed Principle (OCP)

if simple extensions to the requirements force massive changes to the software, then the architecture of that software is bad, and a good software architecture would reduce the amount of changed code to the barest minimum. Ideally, zero.

public enum paymentType = {Cash, CreditCard};public class PaymentManager{
public PaymentType paymentType {get;set;}
public void Pay(Money money){
if(paymentType == PaymentType.cash){
// pay with cash
}else{
// pay with credit card
}
}}

and if we need to add a new payment type? we need to modify this class.

open for extension:

public class payment{public virtual void pay(Money money){// base class
}
}

close for modification:

public class CashPayment:payment{public override void pay(Money money){// pay with cash
}
}public class CreditCard:payment{public override void pay(Money money){// pay with credit card
}
}

Now if you need to add another payment logic you only need to create a new class and implement or extends the payment interface or base class.

If component A should be protected from changes in component B, then component B should depend on component A.

OCP USE-CASE

continuo to the rest of SOLID

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store