Skip to content

Latest commit

 

History

History
176 lines (119 loc) · 3.81 KB

File metadata and controls

176 lines (119 loc) · 3.81 KB

SOLID Principles

Last Updated: 25.02.2026

ocp-banner

Open-closed Principle (OCP) states:

Objects or entities should be open for extension but closed for modification.

👉 Open for Extension
👉 Closed for Modification

This means that:

✅ You should be able to add new behavior
❌ Without changing existing working code

🧠 Why OCP Is Important

In real companies:

  • Features keep changing
  • New business rules come
  • Payment providers change
  • New user roles are added

If every new feature forces you to edit old code → you break existing logic.

That’s dangerous in production systems.

Example

❌ Bad Example (OCP Violation)

Let’s say we are building a payment system.

public class PaymentService {

    public void processPayment(String type) {

        if (type.equals("CREDIT_CARD")) {
            System.out.println("Processing credit card payment");
        }
        else if (type.equals("UPI")) {
            System.out.println("Processing UPI payment");
        }
    }
}

Problem - Now business says: Add PayPal, Add Crypto, Add Net Banking
Every time you must:

  1. Modify this class
  2. Add another else if
  3. Retest everything

🔥 This violates OCP because:

  • Class is NOT closed for modification
  • You must change it to add new behavior

✅ Correct Approach (Following OCP)

Instead of modifying existing class, we extend behavior.

👉 Step 1: Create an Interface

public interface PaymentMethod {
    void pay();
}

👉 Step 2: Create Implementations

public class CreditCardPayment implements PaymentMethod {
    public void pay() {
        System.out.println("Processing credit card payment");
    }
}
public class UpiPayment implements PaymentMethod {
    public void pay() {
        System.out.println("Processing UPI payment");
    }
}

👉 Step 3: Modify PaymentService (Only Once)

public class PaymentService {

    public void processPayment(PaymentMethod paymentMethod) {
        paymentMethod.pay();
    }
}

👉 Step 4: Usage

PaymentService service = new PaymentService();

service.processPayment(new CreditCardPayment());
service.processPayment(new UpiPayment());

👉 Now What Happens If the requirement comes to add new payment method?

We can add new class without modifying existing class by extending the PaymentMethod interface.

❇️ Let's say, we want to add PayPal Payment method.

public class PaypalPayment implements PaymentMethod {
    public void pay() {
        System.out.println("Processing PayPal payment");
    }
}

👉 We DO NOT modify PaymentService, Rather we just extend system.

🔥 That is OCP.

🧪 Interview Deep Insight

🗣️ Interviewer may ask: “How does OCP relate to Strategy Pattern?”
🧑‍💻 Answer: OCP is a principle and Strategy is a design pattern that helps implement OCP

⚡ Important Rule

If you see:

if(type == X)
else if(type == Y)
else if(type == Z)

⚠️ 80% chance OCP is being violated.

📊 Before vs After

Without OCP With OCP
if-else chain Polymorphism
Hard to scale Easy to extend
Risky changes Stable core
Tight coupling Loose coupling

Conclusion 🎯

Open Closed Principle means:

✔ Open for extension
✔ Closed for modification
✔ Use interfaces
✔ Use polymorphism
✔ Avoid modifying stable code
✔ Extend behavior instead

Resources 🔗

🔗 https://www.digitalocean.com/community/conceptual-articles/s-o-l-i-d-the-first-five-principles-of-object-oriented-design#open-closed-principle