What does the Open/Closed Principle (OCP) mean?

Answer

The Open/Closed Principle states that software entities (classes, modules, functions) should be open for extension but closed for modification. This means you should be able to add new behavior without changing existing, tested code. For example, if you have a PaymentProcessor that handles credit cards and you need to add PayPal support, OCP says you should extend the system (by adding a new PayPalPayment class implementing a Payment interface) rather than modifying the existing PaymentProcessor. This reduces the risk of introducing bugs in working code. OCP is typically achieved through polymorphism, interfaces, and design patterns like Strategy, Decorator, and Factory.