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

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.

主站蜘蛛池模板: 富平县| 即墨市| 本溪市| 罗田县| 惠东县| 渭南市| 察隅县| 连南| 兴安县| 株洲市| 夏津县| 英吉沙县| 大方县| 天长市| 海原县| 松阳县| 辉县市| 永平县| 昭觉县| 台湾省| 津南区| 夏邑县| 仁化县| 会理县| 南木林县| 离岛区| 乡宁县| 五指山市| 云林县| 五家渠市| 卓资县| 凤凰县| 三明市| 延津县| 象山县| 灵丘县| 中宁县| 德江县| 大丰市| 五河县| 闽侯县|