Thứ Sáu, 11 tháng 9, 2015

Design Patterns: The Facade Pattern ( Part 1)

To that, my counter question would be: "Would you rather live in a luxurious home, or in a simple establishment with four walls?" After all, both serve our purpose.

Generally, we look for a luxurious home because it offers better facilities and requires less maintenance, and maintenance can done with less hassle because the basic groundwork is already there.

The same thing applies to programming: Code that employs design patterns is easy to understand, easy to maintain, and easy to extend.

In this series of tutorials, we will cover some different design patterns that are available to us in programming. You'll learn about their pros and cons, and factors that indicate where we should use them.

Throughout these tutorials, I will take PHP as a base language to demonstrate design patterns; however, they are actually a concept that can be applied to any programming language—it's just a matter of changing the syntax as per your preferred language.

Design rules are separated into four categories:

creational patterns
structural patterns
behavioral patterns
concurrency patterns
In this tutorial, we are going to cover the facade design pattern. It falls under the category of structural patterns because it deals with how your code should be structured to make it easily intelligible and keep it well maintained in the long term.

Facade Design Pattern
The UML



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?

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.

Example
In this tutorial, let's choose one lesson so that it makes things more readable. Let's say that you have been given a task to plan your friend's marriage. If you do everything on your own, then imagine the things you need to cover. It will create a higher possibility for error, and increase the chance of missing something that can drastically affect your friend's wedding.

In this case, instead of doing everything on your own, you should use a wedding planner and make sure the job gets done in a well-managed manner with less chance of a mistake.

Here, you are behaving as a client who initiates the process, and the wedding planner is working as a "facade" for you, completing the job based on your direction.

Share

& Comment

0 nhận xét:

Đăng nhận xét

 

Copyright © 2015 PHP Scripts - PHP Sources Libraries

Designed by Templateism | Thiết kế website