Facade Design Pattern in C++



Facade Design Pattern is a structural design pattern that provides a single and simplified interface to a complex system of classes, library or framework. It orginizes a complicated system by adding a simple interface to it.

Instead of having multiple objects to interact with, the client can interact with a single facade object which internally manages the interactions with the complex system.

Imagine the following big "Facade" building is like a magic "Play" button for a whole toy factory. You just press that one button, and inside, all the machines start working together automatically to make toys, without you needing to know how each one works.

Facade Design Pattern Example

Components of Facade Design Pattern

The Facade Design Pattern consists of the following three parts −

  • Facade − This is the main part the user talks to. It provides simplified interface to bunch of subsystems. It act as a middleman between client and complex system.
  • Complex System − Many classes that do the real work. These classes or we also call them subsystems are the ones who actually do the tasks. They can be hard to use directly without the facade.
  • Client − The user. Talks only to the facade, not the complex system. Means, the client doesn't need to know about the complex system, it just uses the facade to get things done.
Components of Facade Pattern

Implementation of Facade Design Pattern in C++

Now, let's see how we can implement the Facade Design Pattern in C++.

In this implementation, we will create a simple Home Theater system where we have multiple components like TV, Sound System, and DVD Player. We will then create a Facade class that provides a simplified interface to control these components.

Steps to Implement Facade Design Pattern

Let's see how we can implement the Facade Design Pattern in C++:

For this implementation, we follow 5 steps. First we identify the components of complex system. Then we create classes for each component. After that, we create a facade class that provides a simple interface to interact with the complex system. Next, we implement the methods in the facade class to control the components. Finally, we create a client class to use the facade. Final step is to Keep subsystems independent which is totally optional.

Steps to Implement Facade Design Pattern

C++ Code for Facade Design Pattern

Let's look at the C++ code for the Facade Design Pattern implementation of a Home Theater system.

Here we will use all the components of the Facade Design Pattern that we discussed earlier. We will create classes for each component of the complex system, a facade class that provides a simple interface to interact with the complex system, and a client class to use the facade.

#include <iostream>
using namespace std;
// Complex System Components
class TV {
public:
   void on() {
      cout << "TV is ON" << endl;
   }
   void off() {
      cout << "TV is OFF" << endl;
   }
};
class SoundSystem {
public:
   void on() {
      cout << "Sound System is ON" << endl;
   }
   void off() {
      cout << "Sound System is OFF" << endl;
   }
};
class DVDPlayer {
public:
   void on() {
      cout << "DVD Player is ON" << endl;
   }
   void off() {
      cout << "DVD Player is OFF" << endl;
   }
   void play(string movie) {
      cout << "Playing movie: " << movie << endl;
   }
};
// Facade
class HomeTheaterFacade {
private:
   TV* tv;
   SoundSystem* soundSystem;
   DVDPlayer* dvdPlayer;
public:
   HomeTheaterFacade(TV* t, SoundSystem* s, DVDPlayer* d
) : tv(t), soundSystem(s), dvdPlayer(d) {}
   void watchMovie(string movie) {
      cout << "Get ready to watch a movie..." << endl;
      tv->on();
      soundSystem->on();
      dvdPlayer->on();
      dvdPlayer->play(movie);
   }
   void endMovie() {
      cout << "Shutting down the home theater..." << endl;
      dvdPlayer->off();
      soundSystem->off();
      tv->off();
   }
};
// Client
int main() {
   TV* tv = new TV();
   SoundSystem* soundSystem = new SoundSystem();
   DVDPlayer* dvdPlayer = new DVDPlayer();
   HomeTheaterFacade* homeTheater = new HomeTheaterFacade(tv, soundSystem, dvdPlayer);
   homeTheater->watchMovie("Inception");
   homeTheater->endMovie();
   delete homeTheater;
   delete tv;
   delete soundSystem;
   delete dvdPlayer;
   return 0;
}

Following is the output of the above code:

Get ready to watch a movie...
TV is ON
Sound System is ON
DVD Player is ON
Playing movie: Inception
Shutting down the home theater...
DVD Player is OFF
Sound System is OFF
TV is OFF

Pros and Cons of Facade Design Pattern

The following table highlights the pros and cons of using the Facade Design Pattern

Pros Cons
Gives a simple way to use a complex system. Can become too big if it does too much.
Reduces direct links between client and system. May hide useful features from the client.
Makes code easier to read and manage by hiding complexity. Can make it hard to use advanced features because they are hidden.
Helps in organizing code better. Adds another layer to the system, which may slow things down.
Keeps client and system loosely connected. Can make finding errors harder.

When to Use Facade Design Pattern?

Following are some scenarios where you should consider using the Facade Design Pattern

  • To make a complex system easier to use.
  • For reducing direct connections between client and system.
  • To organize code better.
  • Keeping client and system loosely connected.
  • To hide system details from the client.
  • Making a library or framework simpler to use.
  • Offer a simple interface for common tasks.

Real-world Applications of Facade Design Pattern

Following are some real-world applications of the Facade Design Pattern

Real Life Applications Facade Design Pattern

Conclusion

In this chapter, we learned what the Facade Design Pattern is, its parts, and how to use it effectively. We looked at its good and bad sides, when to use it, and some real uses.

Advertisements