Friday, October 4, 2013

Using Facade Design Pattern

When I doing my internship I came across a project which used Facade Design Pattern. So now I'm going to explain my experience with Facade Design pattern. This post will explain how to use Facade design pattern in simple terms.

Facade design pattern is not a harder pattern. If you are familiar with abstraction and information hiding you can understand it easily. First lets discuss what the meaning of information hiding. Encapsulation is a basic principle of OOP. When we using encapsulation we hide internal properties of an object. Simply we make the attributes of the object private and allow users to get and set their values through getter and setter methods. So the attribute is hidden from you. You can't directly refer that attribute by calling object.attribute (ex: human,name). You have to use human.getName() method to get name of that human object and human.setName("his name") method to set the name of that human object. That is the basic idea of information hiding. Abstraction is also bit similar to information hiding. We use abstraction to hide implementation details. As an example if we call the method car.drive(), that car will drive. But the user does not how the driving process happened. He only know that he called the drive method of that car object, and now the car is driving.

Facade design pattern is the next step to information hiding and abstraction. If you need a combination of few objects to get some work done, you can use Facade pattern to hide that combination information from the user. Think the user gives a command like build a car object. ClientHandler class is the one which handles user request. Building a car is a very complex thing. You need combination of several objects to get that car built. There are several departments in a car factory. Like Building engine and stuff, Building chassis of the car, building tires, assembling department and painting department. So at the ClientHandler class, if you call all these department objects and do the car building, that will be a mess. And that will expose the internal implementation details to the user.
So we introduce a facade class just after the ClientHandler class. In that facade class we can write a method called buildCar() and that will arrange all the department objects and build the car. So the ClientHandler only needs to call buildCar() method of the Facade class. So all the combination and implementation details are hidden from the ClientHandler class.




Hope you got an idea on the Facade Design Pattern and how to use the Facade Design Pattern. What I want with this blog to help my readers to describe complex thing in simple term. There are lots of articles on these topics. But there are not much easier to learn to a beginner. So I want to share my experience with this blog.    

No comments:

Post a Comment