(Structural Design Pattern)
When it come to software development, following a design pattern is must if we need to develop a proper software application
Sometimes, earlier we used this structure of the facade design pattern while development without know the name of it.
It hides the complexity of the code by providing an easy interface to the user that can access the whole system. A single class provides all the methods required by the user from the system.
Implementation with Java
Lets assume a scenario where simple bank transaction process of withdraw money and deposit money.
![]() |
Class diagram for bank transaction |
Step 1:
Create a java application.
Then create an interface for Bank
Bank.java
1 2 3 4 5 6 7 8 9 10 | package facadetestdemo; public interface Bank { CheckAccount checker = new CheckAccount(); public void transaction(double amount); } |
Step 2:
Create additional class for user account checking process. Not an essential part
CheckAccount.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package facadetestdemo; /** * * @author kasun */ public class CheckAccount { private int accountNo = 123456789; private int securityNo = 123; protected double totAmount = 1000; public double getTotalAmount(){ return totAmount; } public int getAccountNumber(){ return accountNo; } public int getSecurityNumber(){ return securityNo; } } |
Step 3:
Now create 2 concrete class for withdraw transaction and deposit transaction.
Withdraw.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class Withdraw implements Bank { @Override public void transaction(double amount) { if(amount > checker.getTotalAmount()){ System.out.println("Sorry, You cannot have enough money to withdraw"); System.out.println("Current Balance: " + checker.getTotalAmount() + "\n"); }else{ checker.totAmount -= amount; System.out.println("Withdrawn is completed: " + amount); System.out.println("Current Balance: " + checker.getTotalAmount() + "\n"); } } } |
Deposit.java
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Deposit implements Bank { @Override public void transaction(double amount) { checker.totAmount += amount; System.out.println("Deposit is completed: " + amount); System.out.println("Current Balance: " + checker.getTotalAmount() + "\n"); } } |
Step 4:
Lets create the facade class
TransactionHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | public class TransactionHandler { private int accountNo = 123456789; private int securityNo = 123; Withdraw withdraw; Deposit deposit; CheckAccount accChecker; public TransactionHandler(int accNo, int secNo){ accountNo = accNo; securityNo = secNo; withdraw = new Withdraw(); deposit = new Deposit(); accChecker = new CheckAccount(); } public void withdrawMoney(double withdrawCash){ if(accountNo == accChecker.getAccountNumber() && securityNo == accChecker.getSecurityNumber()){ System.out.println("Access Granted"); withdraw.transaction(withdrawCash); }else{ System.out.println("Access Dinied: invalid credentials! \n"); } } public void depositMoney(double withdrawCash){ if(accountNo == accChecker.getAccountNumber() && securityNo == accChecker.getSecurityNumber()){ System.out.println("Access Granted"); deposit.transaction(withdrawCash); }else{ System.out.println("Access Dinied: invalid credentials! \n"); } } } |
Step 5:
Now create class (main method) to access methods of the facade class
FacadeTestDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class FacadeTestDemo { /** * @param args the command line arguments */ public static void main(String[] args) { TransactionHandler transHandler = new TransactionHandler(123456789, 123); transHandler.withdrawMoney(1500); transHandler.depositMoney(200); } } |
Step 6:
Lets run the java application
download source code (GitHub): Click here
No comments:
Post a Comment