Hi everyone, I am writing this post to share my knowledge as I continue learning about design patterns. Today, I will present the Factory Method Pattern, which is a design pattern commonly used in real-world applications. If there are any mistakes in my post, please feel free to comment below, and I will gladly fix and update it.
Factory method pattern provides an interface for creating objects in a superclass, but allow subclasses to alter the type of objects that will be created.
Assume you have a bank application, and you’re building a feature for transferring money through various methods like bank transfer, paypal transfer,…
Before using the Factory Method pattern, let’s examine the scenario without it.
I will give an example implemented in Java.
Situation: Person1 sends money to Person2 using a transfer method (Bank Transfer or PayPal Transfer).
Folder structure:
problem/ ├─ BankApp.java ├─ service/ │ ├─ PaypalTransferPayment.java │ ├─ BankTransferPayment.java ├─ data/ │ ├─ Person.java
In the main application, create two persons with default amounts of money.
package problem; import problem.data.Person; public class BankApp { public static void main(String[] args) { Person person1 = new Person("John", 1000); Person person2 = new Person("Jane", 500); } }
Create BankTransferPayment and PaypalTransferPayment classes.
package problem.service; import problem.data.Person; public class BankTransferPayment { public void processPayment(Person fromAccount, Person toAccount, float amount) { fromAccount.withdraw(amount); toAccount.deposit(amount); System.out.println("Bank transfer payment success."); } }
package problem.service; import problem.data.Person; public class PaypalPayment { public void processPayment(Person fromAccount, Person toAccount, float amount) { fromAccount.withdraw(amount); toAccount.deposit(amount); System.out.println("Paypal transfer payment success."); } }
Implement the logic in the main function.
package problem; import problem.data.Person; import problem.service.BankTransferPayment; import problem.service.PaypalPayment; public class BankApp { public static void main(String[] args) { Person person1 = new Person("John", 1000); Person person2 = new Person("Jane", 500); String paymentMethod = "BANK_TRANSFER"; if (paymentMethod.equals("BANK_TRANSFER")) { BankTransferPayment bankTransferPayment = new BankTransferPayment(); bankTransferPayment.processPayment(person1, person2, 100); System.out.println("===Method bank_transfer==="); System.out.println(person1.getName() + " has " + person1.getAmount()); System.out.println(person2.getName() + " has " + person2.getAmount()); } else if (paymentMethod.equals("PAYPAL")) { PaypalPayment paypalPayment = new PaypalPayment(); paypalPayment.processPayment(person1, person2, 100); System.out.println("===Method paypal==="); System.out.println(person1.getName() + " has " + person1.getAmount()); System.out.println(person2.getName() + " has " + person2.getAmount()); } } }
Problems with the current implementation:
The solution to the above situation is to use factory method pattern. So, how do we apply it ?
In the example above:
To solve these issues, the Factory Method pattern will be implemented step by step.
Folder structure (solution):
solution/ ├─ BankApp.java ├─ service/ │ ├─ payments/ │ │ ├─ Payment.java │ │ ├─ PaymentFactory.java │ │ ├─ BankTransferPayment.java │ │ ├─ PaypalTransferPayment.java ├─ data/ │ ├─ Person.java
Step 1: Create Payment interface, declares common method processPayment
package solution.service.payments; import solution.data.Person; // Step 1: Create an interface for the payment public interface Payment { void processPayment(Person fromAccount, Person toAccount,float amount); }
Step 2: Create BankTransferPayment and PaypalTransferPayment classes implement Payment interface.
package solution.service.payments; import solution.data.Person; // Step 2: Create a class that implements the Payment interface public class BankTransferPayment implements Payment { @Override public void processPayment(Person fromAccount, Person toAccount, float amount) { fromAccount.withdraw(amount); toAccount.deposit(amount); System.out.println("Bank transfer payment success."); } }
package solution.service.payments; import solution.data.Person; public class PaypalPayment implements Payment{ @Override public void processPayment(Person fromAccount, Person toAccount, float amount) { fromAccount.withdraw(amount); toAccount.deposit(amount); System.out.println("Paypal transfer payment success."); } }
Step 3: Create PaymentFactory class. This class is responsible for creating objects based on payment type condition.
package solution.service.payments; public class PaymentFactory { public Payment createPayment(String paymentType) { if (paymentType == null) { return null; } if (paymentType.equalsIgnoreCase("BANK_TRANSFER")) { return new BankTransferPayment(); } else if (paymentType.equalsIgnoreCase("PAYPAL")) { return new PaypalPayment(); } return null; } }
Step 4: Use the Factory in the Main Application.
Modify the main function to use the Factory Method pattern.
problem/ ├─ BankApp.java ├─ service/ │ ├─ PaypalTransferPayment.java │ ├─ BankTransferPayment.java ├─ data/ │ ├─ Person.java
To make the PaymentFactory class comply with the Open/Closed Principle (from SOLID principles), you can implement a dynamic registration mechanism using the Strategy Pattern.
Updated PaymentFactory.java:
package problem; import problem.data.Person; public class BankApp { public static void main(String[] args) { Person person1 = new Person("John", 1000); Person person2 = new Person("Jane", 500); } }
Using the Updated Factory in the Main Application.
package problem.service; import problem.data.Person; public class BankTransferPayment { public void processPayment(Person fromAccount, Person toAccount, float amount) { fromAccount.withdraw(amount); toAccount.deposit(amount); System.out.println("Bank transfer payment success."); } }
By applying this approach, the code adheres to the Open/Closed Principle, enabling the addition of new payment methods without modifying the PaymentFactory logic.
I hope this post will be helpful to you.
guru-design-patterns
The above is the detailed content of Understanding the Factory Method Pattern. For more information, please follow other related articles on the PHP Chinese website!