0% found this document useful (0 votes)
30 views

Façade Pattern: Submitted By: Ankit Kumar Jaiswal Sambit Panda Abhinav Jain

The Facade Pattern provides a simplified interface to a more complex subsystem. It hides the complexities of the subsystem from the client. The Facade Pattern is useful when a system is complex to handle, there is a need for abstraction to hide internal complexities, and classes are tightly coupled. The example shows how a TransactionHandler facade class provides simplified withdraw and deposit functionality by interacting with Withdraw and Deposit classes that encapsulate the complex transaction logic. The facade handles authentication and delegates transaction requests to the appropriate subclasses.

Uploaded by

sambitsoft
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Façade Pattern: Submitted By: Ankit Kumar Jaiswal Sambit Panda Abhinav Jain

The Facade Pattern provides a simplified interface to a more complex subsystem. It hides the complexities of the subsystem from the client. The Facade Pattern is useful when a system is complex to handle, there is a need for abstraction to hide internal complexities, and classes are tightly coupled. The example shows how a TransactionHandler facade class provides simplified withdraw and deposit functionality by interacting with Withdraw and Deposit classes that encapsulate the complex transaction logic. The facade handles authentication and delegates transaction requests to the appropriate subclasses.

Uploaded by

sambitsoft
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

+

Façade Pattern

Submitted By:
Ankit Kumar Jaiswal
Sambit Panda
Abhinav Jain
+
Facade Pattern

 A Facade Pattern says that just "just provide a unified and simplified
interface to a set of interfaces in a subsystem, therefore it hides the
complexities of the subsystem from the client".

 In other words, Facade Pattern describes a higher-level interface that makes


the sub-system easier to use.
+
When to use Façade Pattern

 System gets loaded and complex to handle by simple client calls.

 There is a need for abstraction to hide the internal complex procedures.

 Classes of a system are tightly –coupled.

 There are many dependencies between system implementation and the


client.

 There is heavy learning curve to understand the legacy system.

 There is a need for layering to the each subsystem.


+
Facade Pattern
+

Withdrawal And Deposits Process Using


Façade Pattern
+
Example :

 Consider a situation where client requirement is to withdraw and deposit money


to a bank through a transaction system.

 So, the complexities and modules involved in achieving the prime goal of
withdrawal and deposit can be kept in Façade.

 In presented example transaction handler is acting as Façade which will be


responsible for providing solution to the client need.

 So transaction handler will act as simplified interface between multiple


subsystems.
+
+
Step 1: Create an interface for Bank

package facadetestdemo;

public interface Bank {

CheckAccount checker = new


CheckAccount();

public void transaction(double amount);

}
+
Step 2: Create additional class for user
account checking process

package facadetestdemo;
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: Create 2 concrete class for withdraw
transaction and deposit transaction.

public class Withdraw implements Bank {


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");

}
}
}
+
Step 3: Continue.

public class Deposit implements Bank


{

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: Create the facade class

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();

}
+
Step 4: Continue.

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");

}
}
+
Step 4: Continue.

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: Create class (main method) to access
methods of the facade class

public class FacadeTestDemo {

public static void main(String[] args) {

TransactionHandler transHandler = new TransactionHandler(123456789,


123);

transHandler.withdrawMoney(1500);
transHandler.depositMoney(200);
}
}
+
Advantage of Facade Pattern

 It shields the client from dealing with the complicated inner system

 Facilitate loose-coupling to reduce the complexity

 Can use to shield the legacy and poorly designed code which is unable to
refactor

 Ability to introduce more the one façade to one system to layer the subsystems
+
Drawbacks of Façade Design Pattern

 If used without a real need, your system may get further complicated by
unnecessary codes

 Note that Facade does not add any functionality, it just simplifies interfaces
+ 18

Thank you

You might also like