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.
Previous
What does the Single Responsibility Principle (SRP) mean?
Next
What does the Liskov Substitution Principle (LSP) mean?
More SOLID Principles Questions
View all →- Beginner What are the SOLID principles?
- Beginner What does the Single Responsibility Principle (SRP) mean?
- Beginner What does the Liskov Substitution Principle (LSP) mean?
- Beginner What does the Interface Segregation Principle (ISP) mean?
- Beginner What does the Dependency Inversion Principle (DIP) mean?