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.