DIP in Payment System
This program applies DIP to payment system.
Problem Statement
Write a Java program where PaymentProcessor depends on abstraction Payment.
Source Code
| 1 | interface Payment { void pay(); } |
| 2 | class CreditCardPayment implements Payment { |
| 3 | public void pay(){ System.out.println("Paid by Credit Card"); } |
| 4 | } |
| 5 | class PaymentProcessor { |
| 6 | private Payment payment; |
| 7 | PaymentProcessor(Payment p){ payment=p; } |
| 8 | void process(){ payment.pay(); } |
| 9 | } |
| 10 | public class DIPPayment { |
| 11 | public static void main(String[] args) { |
| 12 | System.out.println("Eduinq DIP Payment"); |
| 13 | Payment p=new CreditCardPayment(); |
| 14 | PaymentProcessor pp=new PaymentProcessor(p); |
| 15 | pp.process(); |
| 16 | } |
| 17 | } |
Program Output
Eduinq DIP Payment Paid by Credit Card
Explanation
DIP ensures high level modules depend on abstractions. - PaymentProcessor depends on Payment interface, not implementation. - This promotes loose coupling and flexibility. Conclusion: Shows DIP applied to payment system.