官术网_书友最值得收藏!

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:

UML Diagram for the Factory design pattern

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.

主站蜘蛛池模板: 平江县| 玛曲县| 西乡县| 锡林郭勒盟| 常宁市| 大余县| 北流市| 淮南市| 泸州市| 策勒县| 南阳市| 秦皇岛市| 大方县| 当涂县| 正阳县| 天峻县| 河北省| 泰顺县| 洪湖市| 汝阳县| 雅江县| 芒康县| 平江县| 陆河县| 怀化市| 岳西县| 普兰店市| 婺源县| 萍乡市| 正镶白旗| 灵丘县| 台东县| 隆子县| 江川县| 台山市| 舞阳县| 三明市| 岑巩县| 洞口县| 唐海县| 普洱|