Se202-Software Design and Architecture
Se202-Software Design and Architecture
AND ARCHITECTURE
LECTURE 9
Facade Design Pattern
According to the Gang of Four, the intent of the Facade pattern
is to
Computer-World Example
Think about a situation where you use a method from a library (in the context of a
programming language). You do not care how the method is implemented in the library. You just
call the method to experiment the easy usage of it.
Java Design Patterns, Vaskaran Sarcar
Facade Design Pattern
Facade pattern enables us to use a complex system more easily, either to use just a
subset of the system or use the system in a particular way.
https://www.pearsonhighered.com/assets/samplechapter/0/3/2/1/0321247140.p
df
Example
The startup of a computer is a good example. When a computer starts up, it involves the
work of cpu, memory, hard drive, etc. To make it easy to use for users, we can add a
facade which wrap the complexity of the task, and provide one simple interface instead.
With a Facade object in place, clients interact with the Facade object instead of
interacting directly with subsystem classes.
https://www.programcreek.com/2013/02/java-design-pattern-facade/
Solution
https://www.programcreek.com/2013/02/java-design-pattern-facade/
Example
https://www.dotnettricks.com/learn/designpatterns/facade-design-pattern-dotnet
Solution
https://www.dotnettricks.com/learn/designpatterns/facade-design-pattern-dotnet
Example
BankAccountFacade
-accountNumber:int
-securityCode:int
+acctChecker:AccountNumberCheck
+codeChecker:SecurityCodeCheck
+fundChecker:FundsCheck
+bankWelcome:WelcomeToBank
+bankAccountFacade(newAcctNum:int,
newSecCode:int)
+getAccountNumber():int
+getSecurityCode():int
+withdrawCash(cashToGet:double).void WelcomeToBank
+depositCash(cashToDeposit:double):void
+WelcomeToBank()
-accountNumber:int
-securityCode:int
+acctChecker:AccountNumberCheck
+codeChecker:SecurityCodeCheck
+fundChecker:FundsCheck
+bankWelcome:WelcomeToBank
+bankAccountFacade(newAcctNum:int,
newSecCode:int)
+getAccountNumber():int
+getSecurityCode():int
+withdrawCash(cashToGet:double).void
+depositCash(cashToDeposit:double):void
Example
OrderFacade
-pymnt:Payment
-inventry:Inventory
+placeOrder(OrderId:String, amount:int):void
Inventory Payment
It can also help you to reduce the number of objects that a client needs to
deal with.
Example
https://www.youtube.com/watch?v=dLjJo2v2re8
public class Flight {
public void reserveFlight() {
System.out.println("Flight reserved successfully"); public class TravelPackage {
} private Flight f;
} private Transfer t;
private Hotel h;
public class Transfer {
public void reserveTransfer() { public TravelPackage() {
System.out.println("Transfer reserved successfully"); f=new Flight();
} t=new Transfer();
} h=new Hotel();
}
public class Hotel {
public void reserveHotel() {
public void reserve() {
System.out.println("Hotel reserved successfully");
f.reserveFlight();
}
t.reserveTransfer();
}
h.reserveHotel();
}
public class TestFacade {
}
public static void main(String[] args) {
TravelPackage x=new TravelPackage();
x.reserve();
}
}
References
Java Design Patterns, A Hands-On Experience with Real-World Examples, Vaskaran Sarcar
https://www.pearsonhighered.com/assets/samplechapter/0/3/2/1/0321247140.pdf
https://www.programcreek.com/2013/02/java-design-pattern-facade/
https://www.dotnettricks.com/learn/designpatterns/facade-design-pattern-dotnet
https://www.youtube.com/watch?v=B1Y8fcYrz5o
https://www.youtube.com/watch?v=dLjJo2v2re8