DEV Community

Palanivel SundaraRajan GugaGuruNathan
Palanivel SundaraRajan GugaGuruNathan

Posted on • Originally published at gurupalaniveltech.hashnode.dev on

Dependency Inversion Principle

The Dependency Inversion Principle (DIP) states that high level modules(Payment ) should not depend on low level modules(UpiPayment ,CryptoPayment); both should depend on abstractions(PaymentGateway). Abstractions should not depend on details.

public class Payment {
    public void processPayment(UpiPayment upiPayment) {
        // Implementation for processing UPI payment
    }
}

class UpiPayment {
    // UPI payment related properties and methods
}
Enter fullscreen mode Exit fullscreen mode

Tomm you want to add CryptoPayment you need to modify the Payment class this bad .

Good Practice Principle not followed


public class Payment {
    public void processPayment(PaymentGateway paymentGateway) {
        // Implementation for processing payment via PaymentGateway
    }
}

interface PaymentGateway {
    // Payment gateway related methods
}

class UpiPayment implements PaymentGateway {
    // UPI payment related properties and methods
}

class CryptoPayment implements PaymentGateway {
    // Cryptocurrency payment related properties and methods
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)