Tag Archives: Facade pattern

Facade Pattern

Make it easy.  Provide an easy to use interface for a complex subsystem.

One touch play:

Weekend is up again. You rented a movie to watch and you have a sophisticated Full HD TV and home theatre system. Here are the steps to do to watch a movie.

  1. Turn on TV and home theatre system.facade
  2. Tune in TV to accept input from home theatre; it could be synced to DISH or computer etc.
  3. Change the home theatre mode to DVD as you are about to watch a DVD. It will usually have mode like, FM, TV, external input etc.
  4. Turn all the lights-off
  5. Turn on the popcorn popper for delicious popcorn to eat while watching movie.
  6. Put the DVD in the DVD player tray and play the movie.

What an exhaustive procedure to follow every time you wanted to watch a movie, it’s difficult and mind daunting.

What if you had all these systems are interconnected well with a remote control and on the remote control you had a “One touch play” button.

Just put the DVD on the DVD tray and press the “One touch play” button. All those six step procedures performed automatically. You enjoy the movie peacefully. That “One touch play” button implements Façade pattern.

Definition from GoF:

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

Class diagram:

facade

Participants:
Façade (Remote control)
– Knows which subsystem is responsible for a particular request.
– Delegates client request to appropriate subsystem objects.
Subsystem classes (TV, Home theatre system, popcorn popper and Light system)
– Implement subsystem functionality.
– Handle work assigned by the façade object
– Have no knowledge of the façade; that is, they keep no references to it.

Implementation:

TV class


public class TV
 {
 public void TurnOn()
 {
 Console.WriteLine("TV turning on.");
 }
 public void SyncUpToHomeTheatre()
 {
 Console.WriteLine("TV syncing up to home theatre system.");
 }
 }

Home Theatre system class


public class HomeTheatreSystem
 {
 public void PowerOn()
 {
 Console.WriteLine("Home theatre system turning on.");
 }
 public void SetModeToDVD()
 {
 Console.WriteLine("Change mode to DVD player.");
 }
 public void Play()
 {
 Console.WriteLine("Playing movie");
 }
 }

Light system class:


public class RoomLightSystem
 {
 public void TurnOff()
 {
 Console.WriteLine("Turning of the lights");
 }
 }

Popcorn popper class:


public class PopcornPopper
 {
 public void TurnOn()
 {
 Console.WriteLine("Turning on the popper");
 }
 public void PopIt()
 {
 Console.WriteLine("Popping popcorn");
 }
 }

Remote control class:


public class RemoteControl
 {
 private TV tv = new TV();
 private HomeTheatreSystem hts = new HomeTheatreSystem();
 private RoomLightSystem lights = new RoomLightSystem();
 PopcornPopper popper = new PopcornPopper();
 public void OneTouchPlay()
 {
 tv.TurnOn();
 hts.PowerOn();
 tv.SyncUpToHomeTheatre();
 hts.SetModeToDVD();
 lights.TurnOff();
 popper.TurnOn();
 popper.PopIt();
 hts.Play();
 }
 }

Client code:


class Program
 {
 static void Main(string[] args)
 {
 RemoteControl remote = new RemoteControl();
 remote.OneTouchPlay();
 Console.ReadKey();
 }
 }

Sample output:


TV turning on.
Home theatre system turning on.
TV syncing up to home theatre system.
Change mode to DVD player.
Turning of the lights
Turning on the popper
Popping popcorn
Playing movie

As you can see clients communicate with the subsystem by sending requests to Façade(Remote), which forwards them to the appropriate subsystem objects. Although the subsystem objects perform the actual work, the façade may have to do work of its own to translate its interface to subsystem interfaces.