August 12, 2005

Facade Design Pattern

Design Patterns are recurring solutions for the common software design problems. Knowing design patterns will dramatically improve the way you design your software. These days till late night I’m reading Head First Design Patterns book. Trust me its really good. I think all of the developers out there should have one. ;) So I would like to share some brief explanation to some of the design patters who really took my breath away. I will explain each & every design pattern in my design pattern series. So stay tune. More to come. Today I will explain you the Façade pattern. Its really simple.

What is Façade Pattern?

It’s an interface for an entire subsystem. It’s a high level layer that makes the subsystem easier to use.

Best way to learn is by a sample. So here is the problem. Imagine after hard day work you go home & prepare your self to watch a movie. What are the things that you have to do before you sit down infront of the TV. You have to on the TV, on DVD player, start AC, dim the lights….etc.. What if you can just sit down & do all the things with a remote control. Nice huh.. that’s what exactly façade pattern do.. so the remote control is the façade. You can get the clear picture by a simeple UML diagram & the code below. Comments are highly appreciated. Stay tune for the next design pattern….

HomeTheater facade.

public class HomeTheaterFacade
{

//Has all the objects

DVDPlayer _dvdPlayer;
Light _light;
TV _tv;
AC _ac;

public HomeTheaterFacade()
{
this._ac = new AC();
this._dvdPlayer = new DVDPlayer();
this._light = new Light();
this._tv = new TV();
}

//To watch the movie simply pass the movie
public void WatchMovie(Movie movie)
{
_light.Dim();
_tv.On();
_ac.Start();
_dvdPlayer.Play(movie);
}
}

see how the client use the HometheaterFacade to watch the movie

HomeTheaterFacade e = new HomeTheaterFacade();

Movie movie = new Movie("Matrix Reloaded");
e.WatchMovie(movie);

there more patterns that will imporove your OO designs. so stay tune more to come..

3 comments:

Anonymous said...

very good article
it helped a lot

Thanuj said...

this is very usefull. and please try to explain more in real time scenarios
that is easy to understand.

Anonymous said...

buen artuculo para comenzar