To implement different payment methods (e.g., GPay, PhonePe, Net Banking) using the Factory Design Pattern, we can structure the code to allow flexible, easily extendable payment options. Here’s a step-by-step guide to designing this system.
First, define a common interface for all payment methods, such as Payment
. This interface will declare a method for processing payments.
public interface Payment {
void pay(double amount);
}
Create specific classes for each payment method (GPay, PhonePe, and Net Banking) that implement the Payment
interface.
public class GPay implements Payment {
@Override
public void pay(double amount) {
System.out.println("Paid " + amount + " using GPay.");
}
}
public class PhonePe implements Payment {
@Override
public void pay(double amount) {
System.out.println("Paid " + amount + " using PhonePe.");
}
}
public class NetBanking implements Payment {
@Override
public void pay(double amount) {
System.out.println("Paid " + amount + " using Net Banking.");
}
}
The PaymentFactory
class will contain a method to create instances of different payment methods based on a parameter.
public class PaymentFactory {
public static Payment createPayment(String type) {
if (type.equalsIgnoreCase("gpay")) {
return new GPay();
} else if (type.equalsIgnoreCase("phonepe")) {
return new PhonePe();
} else if (type.equalsIgnoreCase("netbanking")) {
return new NetBanking();
}
throw new IllegalArgumentException("Unknown payment type");
}
}
In your main application code, use the PaymentFactory
to create the appropriate payment method without directly instantiating specific payment classes.
public class Main {
public static void main(String[] args) {
// Use the factory to get the desired payment method
Payment paymentMethod1 = PaymentFactory.createPayment("gpay");
paymentMethod1.pay(1000); // Output: Paid 1000.0 using GPay.
Payment paymentMethod2 = PaymentFactory.createPayment("phonepe");
paymentMethod2.pay(500); // Output: Paid 500.0 using PhonePe.
Payment paymentMethod3 = PaymentFactory.createPayment("netbanking");
paymentMethod3.pay(2000); // Output: Paid 2000.0 using Net Banking.
}
}
- Encapsulation: The factory hides the instantiation logic of different payment methods.
- Extensibility: Adding a new payment method (e.g., PayPal) only requires adding a new class and updating the
PaymentFactory
. - Flexibility: The client code is decoupled from the specific implementations of each payment method and only interacts through the
Payment
interface.
This approach makes managing and extending the payment options easy, reducing dependencies and improving maintainability.