One of the biggest challenges in software development is keeping systems flexible and scalable without breaking existing functionality. This is where software design principles come in, helping developers build robust and maintainable applications.
A key principle is the Open/Closed Principle (OCP), part of SOLID, which states:
“Software entities should be open for extension but closed for modification.”
This means your code should allow adding new functionality without changing existing code.
Real-World Problem
Imagine a payment processing system. At first, it only accepts credit cards, but later, users request PayPal and cryptocurrency payments.
- Bad design: constantly modifying the same class, increasing the risk of errors.
- Good design with OCP: create new classes to extend functionality without touching existing code.
Example in Java
// Common interface for any payment method
interface PaymentMethod {
void pay(double amount);
}
// Initial implementation: Credit Card
class CreditCardPayment implements PaymentMethod {
@Override
public void pay(double amount) {
System.out.println("Paying $" + amount + " with Credit Card.");
}
}
// New implementation: PayPal
class PayPalPayment implements PaymentMethod {
@Override
public void pay(double amount) {
System.out.println("Paying $" + amount + " with PayPal.");
}
}
// New implementation: Cryptocurrency
class CryptoPayment implements PaymentMethod {
@Override
public void pay(double amount) {
System.out.println("Paying $" + amount + " with Cryptocurrency.");
}
}
// PaymentProcessor class WITHOUT modifying existing code
class PaymentProcessor {
private final PaymentMethod paymentMethod;
public PaymentProcessor(PaymentMethod paymentMethod) {
this.paymentMethod = paymentMethod;
}
public void process(double amount) {
paymentMethod.pay(amount);
}
}
// Demo main
public class Main {
public static void main(String[] args) {
PaymentProcessor creditCard = new PaymentProcessor(new CreditCardPayment());
creditCard.process(100);
PaymentProcessor paypal = new PaymentProcessor(new PayPalPayment());
paypal.process(200);
PaymentProcessor crypto = new PaymentProcessor(new CryptoPayment());
crypto.process(300);
}
}
Expected Output
Paying $100 with Credit Card.
Paying $200 with PayPal.
Paying $300 with Cryptocurrency.
Design Explanation
- PaymentMethod interface: defines a contract all payment types must follow.
- Concrete classes (CreditCardPayment, PayPalPayment, CryptoPayment): implement the contract.
- PaymentProcessor: executes any implementation of PaymentMethod.
- Extensibility: if a new method like Apple Pay is added, just create a new class implementing the interface—no existing code is modified.
Benefits of the Open/Closed Principle
✔️ Fewer errors: existing working code remains untouched.
✔️ Reusability: new classes only implement the common interface.
✔️ Scalability: easily add new functionality.
✔️ Maintainability: code is modular and clear.
Conclusion
The Open/Closed Principle (OCP) teaches developers how to design systems that grow without breaking existing functionality. This Java example demonstrates how a payment system can adapt to new methods by extending the code with new classes.
Applying OCP results in software that is robust, flexible, and ready for future changes.
Top comments (0)