Sitemap
Balkrishna Shukla

Balkrishna Shukla

Software Engineer

Facade Design Pattern

3 min readMay 6, 2023
Press enter or click to view image in full size

The Facade design pattern is a structural pattern that provides a simple interface to a complex subsystem. It allows clients to interact with a complex system using a simple interface, without having to understand the complexities of the subsystem.

Imagine a home theater system with multiple components such as a projector, screen, speakers, amplifier, and DVD player. Without Using this pattern, the client have to interact with each of these components individually and handle their initialization and calling their methods . This could become very complex and error-prone, especially if the client is not familiar with the technical details of each component.

However, with a Facade, the client can interact with a simplified interface that encapsulates complexities of the subsystem. The Facade would provide methods such as startHomeTheater() and stopHomeTheater() that handle the initialization and coordination of the components.

//...............................Projector Class........................
public class Projector {
public void on() {
System.out.println("Turning on the projector");
}

public void off() {
System.out.println("Turning off the projector");
}
}
//...............................Screen Class........................
public class Screen {
public void lower() {
System.out.println("Lowering the screen.");
}

public void raise() {
System.out.println("Raising the screen.");
}
}
//...............................Speakers Class........................
public class Speakers {
public void on() {
System.out.println("Turning on the speakers.");
}

public void off() {
System.out.println("Turning off the speakers.");
}
}
//...............................Amplifier Class........................
public class Amplifier {
public void on() {
System.out.println("Turning on the amplifier.");
}

public void off() {
System.out.println("Turning off the amplifier");
}
}
//...............................DVDPlayer Class........................
public class DVDPlayer {
public void on() {
System.out.println("Turning On the DVD player");
}

public void off() {
System.out.println("Turning off the DVD player");
}

public void play() {
System.out.println("Play the DVD");
}

public void stop() {
System.out.println("Stop the DVD");
}

public void eject() {
System.out.println("Eject the DVD");
}
}

In the code above each of the class are the part of home theater. The home theater has DVDPlayer, Amplifier, Speakers, Screen, Projector. Now to start the theater I have to perform some tasks in all the subsystems (DVDPlayer, Amplifier, Speakers, Screen, Projector).

Now I will create a facade or wrapper which will take care of initializing and calling these subsystems.

//--this is the facade interface
public interface HomeTheaterFacade {
void startHomeTheater();
void stopHomeTheater();
}
//----this is the implementation of facade interface
public class HomeTheater implements HomeTheaterFacade {
private Projector projector;
private Screen screen;
private Speakers speakers;
private Amplifier amplifier;
private DVDPlayer dvdPlayer;

public HomeTheater() {
projector = new Projector();
screen = new Screen();
speakers = new Speakers();
amplifier = new Amplifier();
dvdPlayer = new DVDPlayer();
}

public void startHomeTheater() {
projector.on();
screen.lower();
speakers.on();
amplifier.on();
dvdPlayer.on();
dvdPlayer.play();
}

public void stopHomeTheater() {
projector.off();
screen.raise();
speakers.off();
amplifier.off();
dvdPlayer.stop();
dvdPlayer.eject();
dvdPlayer.off();
}
}

// This the client code
public class Client {
public static void main(String[] args) {
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
homeTheaterFacade.startHomeTheater();

homeTheaterFacade.stopHomeTheater();
}
}

As you can see in the above code, I have instantiated all the subsytem class in HomeTheater constructor and then provide wrapper method startHomeTheater() and stopHomeTheater() which basically calls all the subsystems method according to the requirement. Like to start a home theater I have to call on() method of Projector class, lower() method of Screen class, on() method of Speakers class, on() method of Amplifier, on() and play() method of DVDPlayer class.

Get Balkrishna Shukla’s stories in your inbox

Join Medium for free to get updates from this writer.

Pros and Cons :-

✔ You can isolate your code from the complexity of a subsystem .

✗ A facade can become a god object coupled to all classes of an app.

References :-

Head first design patterns

If you want to learn about investing then you can use this aff link to buy https://www.amazon.in/Intelligent-Investor-English-Paperback-2013/dp/0062312685/ref=sr_1_3?crid=3LSSVMQGLHRZL&dib=eyJ2IjoiMSJ9.Vnfz-vXY3wywc6NKw4i-uGQLAQG4d16gH-gLAVkX3ByBem3byircsJv527T4qXeJLR-PhHt9TBZHVIolvUWQz-U_BNdIY4E1cdsZho9gWhruHKZ6L6vJ4CBPv0u8GpeA4Ig2PZzIzJxqIULJVaKaNM_Oi5Dc1dEkGIlrBF4id6m4AyuAH1lswZ2wcVhBnGoHKkvi2dp_heVM1hm77VSLMoF0-8goDKQ0nybuaEu65PM.3hWTSWDsh4tLHduy4W-jy6rmF0bttGR3iRACTABm6WE&dib_tag=se&keywords=intelligent+investor&qid=1712160164&sprefix=intelligent%252Caps%252C553&sr=8-3&_encoding=UTF8&tag=bks1409-21&linkCode=ur2&linkId=0e0019e18e610f514ba58c6fe632c508&camp=3638&creative=24630

--

--