- 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(); } }
- 多媒體CAI課件設計與制作導論(第二版)
- Data Visualization with D3 4.x Cookbook(Second Edition)
- WildFly:New Features
- JavaScript高效圖形編程
- iOS應用逆向工程(第2版)
- Python編程從0到1(視頻教學版)
- Visual C++開發入行真功夫
- 基于ARM Cortex-M4F內核的MSP432 MCU開發實踐
- Learning Hadoop 2
- Python入門很輕松(微課超值版)
- Ext JS 4 Plugin and Extension Development
- Python編程快速上手2
- Groovy 2 Cookbook
- 和孩子一起學編程:用Scratch玩Minecraft我的世界
- Mastering Object:Oriented Python(Second Edition)