- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Input Output Operations
- C++ Basic Input/Output
- C++ Cin
- C++ Cout
- C++ Manipulators
- Type System & Data Representation
- C++ Modifier Types
- C++ Storage Classes
- C++ Constexpr Specifier
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Scope Resolution Operator
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ Jump Statements
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Return Values
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Array Decay
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Destructors
- C++ Virtual Destructor
- C++ Inheritance
- C++ Inheritance
- C++ Multiple Inheritance
- C++ Multilevel Inheritance
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ Override Specifiers
- C++ Final Specifiers
- C++ Design Patterns
- C++ Creational Design Patterns
- C++ Singleton Design Pattern
- C++ Factory Method Design Pattern
- C++ Abstract Factory Pattern
- C++ Prototype Design Pattern
- C++ Structural Design Patterns
- C++ Facade Design Pattern
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Move Semantics
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ nullptr
- C++ unordered_multiset
- C++ Structural Design Patterns
- C++ Adapter Pattern
- C++ Bridge Pattern
- C++ Composite Pattern
- C++ Decorator Pattern
- C++ Flyweight Pattern
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.
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.
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.
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 −
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.