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
}
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
}
Top comments (0)