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

Implementing the Facade design pattern

Let's look into the following listings to demonstrate the Facade design pattern.

Create subsystem service classes for your Bank application: Let's see the following PaymentService class for the subsystem.

Following is the PaymentService.java file:

    package com.packt.patterninspring.chapter3.facade.pattern; 
    public class PaymentService { 
      public static boolean doPayment(){ 
         return true; 
      } 
    } 

Let's create another service class AccountService for the subsystem.

Following is the AccountService.java file:

   package com.packt.patterninspring.chapter3.facade.pattern; 
   import com.packt.patterninspring.chapter3.model.Account; 
   import com.packt.patterninspring.chapter3.model.SavingAccount; 
   public class AccountService { 
     public static Account getAccount(String accountId) { 
        return new SavingAccount(); 
     } 
   } 

Let's create another service class TransferService for the subsystem.

Following is the TransferService.java file:

    package com.packt.patterninspring.chapter3.facade.pattern; 
    import com.packt.patterninspring.chapter3.model.Account; 
    public class TransferService { 
      public static void transfer(int amount, Account fromAccount,
Account toAccount) { System.out.println("Transfering Money"); } }

Create a Facade Service class to interact with the subsystem: Let's see the following Facade interface for the subsystem and then implement this Facade interface as a global banking service in the application.

Following is the BankingServiceFacade.java file:

    package com.packt.patterninspring.chapter3.facade.pattern; 
    public interface BankingServiceFacade { 
       void moneyTransfer(); 
    } 

Following is the BankingServiceFacadeImpl.java file:

    package com.packt.patterninspring.chapter3.facade.pattern; 
    import com.packt.patterninspring.chapter3.model.Account; 
    public class BankingServiceFacadeImpl implements 
BankingServiceFacade{ @Override public void moneyTransfer() { if(PaymentService.doPayment()){ Account fromAccount = AccountService.getAccount("1"); Account toAccount = AccountService.getAccount("2"); TransferService.transfer(1000, fromAccount, toAccount); } } }

Create the client of the Facade:

Following is the FacadePatternClient.java file:

    package com.packt.patterninspring.chapter3.facade.pattern; 
    public class FacadePatternClient { 
      public static void main(String[] args) { 
        BankingServiceFacade serviceFacade = new 
BankingServiceFacadeImpl(); serviceFacade.moneyTransfer(); } }
主站蜘蛛池模板: 乌兰浩特市| 贵定县| 建瓯市| 石家庄市| 海城市| 大竹县| 永吉县| 常宁市| 剑阁县| 宁安市| 云梦县| 岳池县| 雷波县| 班玛县| 凌海市| 阿城市| 闽侯县| 西和县| 财经| 福建省| 铜山县| 准格尔旗| 泗水县| 陈巴尔虎旗| 上蔡县| 广昌县| 信阳市| 平顶山市| 灌云县| 达拉特旗| 凉山| 剑河县| 苏州市| 景洪市| 哈巴河县| 临夏市| 德安县| 文安县| 朝阳县| 沙湾县| 柳江县|