Open/Closed Principle
Add new behavior by adding code, not by changing code that already works.
What is Open/Closed Principle?
The Open/Closed Principle says code should be open for extension but closed for modification: you add new behavior by adding new classes, not by editing working ones.
public interface IDiscount { decimal Apply(decimal price); }
// New discount? Add a class; the checkout code stays untouched.
public class StudentDiscount : IDiscount
{
public decimal Apply(decimal price) => price * 0.9m;
}
Level: Freelancer · More OOP terms at this level
Updated