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

Se202-Software Design and Architecture

The Facade design pattern provides a simplified interface to a more complex subsystem. The BankAccountFacade class acts as the facade, hiding the complexity of interacting with multiple subsystem classes like AccountNumberCheck, SecurityCodeCheck, and FundsCheck. It provides easier methods like withdrawCash and depositCash that handle validation and updating funds behind the scenes. WelcomeToBank is responsible for greeting the user. The facade enables clients to interact with the subsystem through a single, unified interface rather than multiple subsystem classes directly.

Uploaded by

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

Se202-Software Design and Architecture

The Facade design pattern provides a simplified interface to a more complex subsystem. The BankAccountFacade class acts as the facade, hiding the complexity of interacting with multiple subsystem classes like AccountNumberCheck, SecurityCodeCheck, and FundsCheck. It provides easier methods like withdrawCash and depositCash that handle validation and updating funds behind the scenes. WelcomeToBank is responsible for greeting the user. The facade enables clients to interact with the subsystem through a single, unified interface rather than multiple subsystem classes directly.

Uploaded by

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

SE202-SOFTWARE DESIGN

AND ARCHITECTURE

LECTURE 9
Facade Design Pattern
 According to the Gang of Four, the intent of the Facade pattern
is to

 Provide a unified interface to a set of interfaces in a subsystem.


Facade defines a higher-level interface that makes the subsystem
easier to use.

 Basically, this is saying that we need to interact with a system that


is easier than the current method, or we need to use the system in
a particular way (such as using a 3D drawing program in a 2D
way). We can build such a method of interaction because we only
need to use a subset of the system in question.
Facade Design Pattern
 Facades make a client’s life easier. Suppose that there is a
complex system where multiple objects need to perform a series
of tasks, and you need to interact with the system.

 In a situation like this, facade can provide you a simplified


interface that takes care of everything (the creation of those
objects, providing the correct sequence of tasks, etc.).

 As a result, instead of interacting with multiple objects in a


complicated way, you just interact with a single object.

 The Facade pattern is useful in such situations. The Façade


pattern provides a higher level, simplified interface for a
subsystem resulting in reduced complexity and dependency. This
in turn makes the subsystem usage easier and more manageable.

Software architecture design patterns in java, Partha Kuchana


Facade Design Pattern
 Real-World Example
 Suppose that you are going to organize a birthday party, and you plan to invite 500 people.
Nowadays, you can go to any party organizer and let them know the key information—
◼ party type,
◼ the date and time,
◼ number of attendees, and so forth.
 The organizer does the rest for you.
 You do not need to think about
◼ how the hall will be decorated,
◼ whether attendees will get their food from a buffet table or be served by the waiter, and so forth.
 So, you do not need to buy items from the store or decorate the party hall yourself—you just
pay the organizer and let them do the job properly.

 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.

 We have a complicated system of which we need to use only a part. We end up


with a simpler, easier-to-use system or one that is customized to our needs.

 The Facade provides a collection of easier-to-understand methods. These methods


use the underlying system to implement the newly defined functions.

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

The classes, interfaces, and objects in the above class diagram


can be identified as follows:
1.CarModel, CarEngine, CarBody, CarAccessories - These
are subsystems.
2.CarFacade- Facade class.
SetModel(): prints «CarModel-SetModel»
SetEngine(): prints «CarEngine-SetEngine» Test Program:
SetBody():«CarBody-SetBody» 1)Create a CarFacade object
SetAccessories(): prints «CarAccessories-SetAccessories» 2)Call the CreateCompleteCar() method.

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

FundsCheck SecurityCodeCheck AccountNumberCheck

-cashInAccount:double // 1000 -securityCode:int // 1234 -accountNumber:int // 987


+getCashInAccount():double +getSecurityCode():int +getAccountNumber():int
+decreaseCashInAccount(cashWithdrawn:double):void +isCodeCorrect(secCodeToCheck:int):boolean +accountActive(acctNumToCheck:int):boolean
+increaseCashInAccount(cashDeposited:double):void
+haveEnoughMoney(cashWithdrawal:double):boolean
+makeDeposit(cashToDeposit:double):void
Example *constructor: makes assignments and create new
WelcomeToBank, AccountNumberCheck,
SecurityCodeCheck,FundsCheck objects.
*getAccountNumber(): returns account number
BankAccountFacade *getSecurityCode(): returns security code
*withdrawCash(cashToGet): if account is active
-accountNumber:int and code is correct and have enough money ,
-securityCode:int the transaction is completed, otherwise
+acctChecker:AccountNumberCheck transaction failed.
+codeChecker:SecurityCodeCheck *depositCash(cashToDeposit): if account is
+fundChecker:FundsCheck active and code is correct, make deposit and
+bankWelcome:WelcomeToBank display transaction complete message
+bankAccountFacade(newAcctNum:int, otherwise transaction failed.
*decreaseCashInAccount(): decreases the cash
newSecCode:int)
*increaseCashInAccount(): increases the cash
+getAccountNumber():int WelcomeToBank
*haveEnougMoney(cashToWithdrawal): checks
+getSecurityCode():int
if there is enough money in the account, if the
+withdrawCash(cashToGet:double).void
answer is «yes», decreases cash in the account
+depositCash(cashToDeposit:double):void +WelcomeToBank()
shows the current money to the customer,
otherwise displays an error message and again
shows the current money. *WelcomeToBank(): inside the
*makeDeposit(cashToDeposit): increases the constructor display a welcome message
cash in the account, and displays current money
SecurityCodeCheck
AccountNumberCheck
FundsCheck -securityCode:int // 1234
-accountNumber:int // 987
+getSecurityCode():int
-cashInAccount:double // 1000 +getAccountNumber():int
+isCodeCorrect(secCodeToCheck:int):boolean
+accountActive(acctNumToCheck:int):boolean
+getCashInAccount():double
+decreaseCashInAccount(cashWithdrawn:double):void *getSecurityCode: returns the security code
+increaseCashInAccount(cashDeposited:double):void *getAccountNumber: returns the account number
*isCodeCorrect(secCodeToCheck): compares *accountActive(acctNumToCheck): compares the
+haveEnoughMoney(cashWithdrawal:double):boolean the security codes if they are equal return
+makeDeposit(cashToDeposit:double):void account numbers if they are equal return true
true otherwise return false otherwise return false
Solution
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
+depositCash(cashToDeposit:double):void
Example

OrderFacade
-pymnt:Payment
-inventry:Inventory
+placeOrder(OrderId:String, amount:int):void

Inventory Payment

+checkInventory(OrderId:String, OrderAmount:int):String +deductPayment(OrderId:String):String


Solution
What are key advantages of using a
facade pattern?
 If a system consists of many subsystems, managing all those subsystems
becomes very tough and clients may find their life difficult to communicate
separately with each of these subsystems. In this scenario, facade patterns
are very much handy. It provides a simple interface to clients. In simple
words, instead of presenting complex subsystems, you present one
simplified interface to clients. This approach also promotes weak coupling
by separating a client from the subsystems.

 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

You might also like