- Spring 5 Design Patterns
- Dinesh Rajput
- 454字
- 2021-07-08 09:59:31
Sample implementation of the Factory design pattern
There are two classes SavingAccount and CurrentAccount implementing an interface Account. So, you can create a Factory class with a method that takes one or more arguments and its return type is Account. This method is known as the Factory method because it creates the instances of either CurrentAccount or SavingAccount. The Account interface is used for loose coupling. So, according to the passed arguments in the factory method, it chooses which subclass to instantiate. This factory method will have the superclass as its return type:

Let's look at this design pattern in the following example. Here, I am going to create an Account interface and some concrete classes that implement the Account interface:
package com.packt.patterninspring.chapter2.factory;
public interface Account { void accountType(); }
Now let's create SavingAccount.java, which will implement the Account interface:
package com.packt.patterninspring.chapter2.factory; public class SavingAccount implements Account{ @Override public void accountType() { System.out.println("SAVING ACCOUNT"); } }
Same with CurrentAccount.java, it will also implement the Account interface:
package com.packt.patterninspring.chapter2.factory; public class CurrentAccount implements Account { @Override public void accountType() { System.out.println("CURRENT ACCOUNT"); } }
A Factory class AccountFactory is now going to be defined. AccountFactory generates an object of the concrete class, either SavingAccount or CurrentAccount, based on the account type given as an argument to the Factory method:
AccountFactory.java is a Factory to produce the Account type object:
package com.packt.patterninspring.chapter2.factory.pattern; import com.packt.patterninspring.chapter2.factory.Account; import com.packt.patterninspring.chapter2.factory.CurrentAccount; import com.packt.patterninspring.chapter2.factory.SavingAccount; public class AccountFactory { final String CURRENT_ACCOUNT = "CURRENT"; final String SAVING_ACCOUNT = "SAVING"; //use getAccount method to get object of type Account //It is factory method for object of type Account public Account getAccount(String accountType){ if(CURRENT_ACCOUNT.equals(accountType)) { return new CurrentAccount(); }
else if(SAVING_ACCOUNT.equals(accountType)){ return new SavingAccount(); } return null; } }
FactoryPatternMain is the main calling class of AccountFactory to get an Account object. It will pass an argument to the factory method that contains information of the account type, such as SAVING and CURRENT. AccountFactory returns the object of the type that you passed to the factory method.
Let's create a demo class FactoryPatterMain.java to test the factory method design pattern:
package com.packt.patterninspring.chapter2.factory.pattern; import com.packt.patterninspring.chapter2.factory.Account; public class FactoryPatterMain { public static void main(String[] args) { AccountFactory accountFactory = new AccountFactory(); //get an object of SavingAccount and call its accountType()
method. Account savingAccount = accountFactory.getAccount("SAVING"); //call accountType method of SavingAccount savingAccount.accountType(); //get an object of CurrentAccount and call its accountType()
method. Account currentAccount = accountFactory.getAccount("CURRENT"); //call accountType method of CurrentAccount currentAccount.accountType(); } }
You can test this file and see the output on the console, which should look like this:

Now that we've seen the Factory design pattern, let's turn to a different variant of it-the Abstract factory design pattern.
- 青少年美育趣味課堂:XMind思維導圖制作
- C語言程序設計
- 薛定宇教授大講堂(卷Ⅳ):MATLAB最優化計算
- Java Web應用開發技術與案例教程(第2版)
- Instant Ext.NET Application Development
- ExtJS高級程序設計
- C語言從入門到精通
- Xcode 6 Essentials
- Drupal 8 Development Cookbook(Second Edition)
- Access數據庫應用教程(2010版)
- C# 7.1 and .NET Core 2.0:Modern Cross-Platform Development(Third Edition)
- 和孩子一起學編程:用Scratch玩Minecraft我的世界
- R語言與網站分析
- Switching to Angular 2
- 零基礎學Java(升級版)