Presented by,
Ms. Nandana S V
Team Lead, Sr Software Engineer, Livares Technologies
Facade design pattern
Facade is a part of Gang of Four design pattern (23 others).
As the name suggests, it means the face of the building.
The people walking past the road can only see this glass face of the building.
They do not know anything about it, the wiring, the pipes and other complexities.
It hides all the complexities of the building and displays a friendly face.
Facade design pattern
Same goes for the Facade Design Pattern. It hides the complexities of the system and provides an interface to the client from where the client can access the system.
2. Design Patterns
● Design patterns are used to represent some of the best practices adapted
by experienced object-oriented software developers.
● A design pattern systematically names, motivates, and explains a general
design that addresses a recurring design problem in object-oriented
systems.
● It describes the problem, the solution, when to apply the solution, and its
consequences.
3. Design Patterns
● It also gives implementation hints and examples.
● The patterns typically show relationships and interactions between classes
or objects.
● The idea is to speed up the development process by providing tested,
proven development paradigm.
4. Goal of Design Patterns
○ Understand the problem and matching it with some pattern.
○ Re Usage of old interface
○ Making the present design reusable for the future usage
5. Types of Design Patterns
○ Creational
■ These design patterns are all about class instantiation or object creation. Trying to
create objects in a manner suitable to the situation
■ Eg: Factory Method,Builder, Singleton
○ Behavioral
■ Behavioral patterns are about identifying common communication patterns between
objects and realize these patterns.
■ Eg: Command, Interpreter, Iterator
6. Types of Design Patterns
○ Structural
■ These design patterns are about organizing different classes and
objects to form larger structures and provide new functionality.
■ Eg:Adapter, Bridge,Facade
8. Facade design pattern
● Facade is a part of Gang of Four design pattern (23 others).
● As the name suggests, it means the face of the building.
● The people walking past the road can only see this glass face of the
building.
● They do not know anything about it, the wiring, the pipes and other
complexities.
● It hides all the complexities of the building and displays a friendly face.
9. Facade design pattern
● Same goes for the Facade Design Pattern. It hides the complexities of the
system and provides an interface to the client from where the client can
access the system.
10.
11.
12. When Should this pattern be used?
● The facade pattern is appropriate when you have a complex system that you
want to expose to clients in a simplified way
● or you want to make an external communication layer over an existing
system which is incompatible with the system.
● Facade deals with interfaces, not implementation.
● Its purpose is to hide internal complexity behind a single interface that
appears simple on the outside.
13. When Should this pattern be used?
● It deals with how your code should be structured to make it easily intelligible
and keep it well maintained in the long term.
14. Problem
Let's assume that you have a few operations to be made in sequence, and that
the same action is required in multiple places within your application. You have
to place the same code again and again in different places. You have done that,
but after a few days you find that something needs to be changed in that code.
Do you see the problem? We have to introduce the changes in all of the places
that the code exists. It's painful, isn't it?
15. Solution
As a solution, what you should be doing is to create a lead controller, which
handles all of the repeating code. From the calling point of view, we will just call
the lead controller to perform actions based on the parameters provided.
Now if we need to introduce any change in the process, then we will just need to
change the lead controller instead of making the change in all places where we
used that code.
16. Code Example
In this section we will see one more example, which is very common for
websites, of course with a code example. We will see an implementation of the
facade design pattern using a product checkout process. But before checking
perfect code with the facade pattern, let's have a look at some code that has a
problem.
17. Code Example
A simple checkout process includes the following steps:
1. Add product to cart.
2. Calculate shipping charge.
3. Calculate discount.
4. Generate order.
18. Code Example - Simple CheckOut Process
$productID = $_GET['productId'];
$qtyCheck = new productQty();
if($qtyCheck->checkQty($productID) > 0) {
$addToCart = new addToCart($productID); // Add Product to Cart
$shipping = new shippingCharge(); // Calculate Shipping Charge
$shipping->updateCharge();
19. Code Example - Simple CheckOut Process
$discount = new discount(); // Calculate Discount Based on
$discount->applyDiscount();
$order = new order();
$order->generateOrder();
}
20. Code Example - Simple CheckOut Process
In the above code, you will find that the checkout procedure includes various
objects that need to be produced in order to complete the checkout operation.
Imagine that you have to implement this process in multiple places. If that's the
case, it will be problematic when the code needs to be modified. It's better to
make those changes in all places at once.
21. Code Example - Facade design checkout process
We will write the same thing with the facade pattern, which makes the same
code more maintainable and extendable.
22. Code Example - Facade design checkout process
class productOrderFacade {
public $productID = '';
public function __construct($pID) {
$this->productID = $pID;
}
23. Code Example - Facade design checkout process
private function addToCart () {
/* .. add product to cart .. */
}
24. Code Example - Facade design checkout process
private function qtyCheck() {
$qty = 'get product quantity from database';
if($qty > 0) {
return true;
} else {
return true;
}
}
25. Code Example - Facade design checkout process
private function calulateShipping() {
$shipping = new shippingCharge();
$shipping->calculateCharge();
}
private function applyDiscount() {
$discount = new discount();
$discount->applyDiscount();
}
26. Code Example - Facade design checkout process
private function placeOrder() {
$order = new order();
$order->generateOrder();
}
27. Code Example - Facade design checkout process
public function generateOrder() {
if($this->qtyCheck()) {
$this->addToCart();// Add Product to Cart
$this->calulateShipping();// Calculate Shipping Charge
$this->applyDiscount();// Calculate Discount if any
$this->placeOrder();// Place and confirm Order
}
}
28. Code Example - Facade design checkout process
As of now, we have our product order facade ready. All we have to do is use it
with a few communication channels of code, instead of a bunch of code as
expressed in the previous part.
Please check the amount of code below which you will need to invest in order to
have a checkout process at multiple positions.
29. Code Example - Facade design checkout process
// Note: We should not use direct get values for Database queries to prevent SQL
injection
$productID = $_GET['productId'];
// Just 2 lines of code in all places, instead of a lengthy process everywhere
$order = new productOrderFacade($productID);
$order->generateOrder();
30. Code Example - Facade design checkout process
Now imagine when you need to make alterations in your checkout process. Now
you simply create changes in the facade class that we have created, rather than
introducing changes in every place where it has been applied.