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

  • Spring 5 Design Patterns
  • Dinesh Rajput
  • 236字
  • 2021-07-08 09:59:40

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(); } }
主站蜘蛛池模板: 太和县| 虎林市| 茌平县| 安福县| 晋中市| 和平县| 清涧县| 金川县| 龙江县| 金坛市| 临洮县| 砀山县| 衡东县| 阳朔县| 凤城市| 贵阳市| 涿州市| 威海市| 西充县| 兴海县| 娱乐| 格尔木市| 湘阴县| 武宁县| 垫江县| 玉树县| 岑巩县| 庆城县| 潼关县| 昭通市| 永寿县| 玉溪市| 英山县| 平原县| 鄂尔多斯市| 平昌县| 哈巴河县| 奎屯市| 新泰市| 巴彦县| 灵台县|