Open In App

Facade Method – C++ Design Patterns

The Facade Pattern is a design pattern in software engineering that falls under the structural pattern category. It provides a simplified and unified interface to a set of interfaces or subsystems within a larger system, making it easier to use and reducing the complexity of the system for clients. Essentially, it acts as a facade or entry point to a more complex system, shielding clients from its intricacies.

Imagine you have a complex software system with numerous components, each having its own set of methods and interactions. Without the Facade Pattern, clients would need to interact with these components directly, which could lead to a tangled mess of dependencies and make the system difficult to understand and maintain.



Implementation of the Facade Pattern in C++ Design Pattern

Below is the implementation of the Facade Pattern in C++:




#include <iostream>
 
// Subsystem 1
class Engine {
public:
    void Start()
    {
        std::cout << "Engine started" << std::endl;
    }
 
    void Stop()
    {
        std::cout << "Engine stopped" << std::endl;
    }
};
 
// Subsystem 2
class Lights {
public:
    void TurnOn() { std::cout << "Lights on" << std::endl; }
 
    void TurnOff()
    {
        std::cout << "Lights off" << std::endl;
    }
};
 
// Facade
class Car {
private:
    Engine engine;
    Lights lights;
 
public:
    void StartCar()
    {
        engine.Start();
        lights.TurnOn();
        std::cout << "Car is ready to drive" << std::endl;
    }
 
    void StopCar()
    {
        lights.TurnOff();
        engine.Stop();
        std::cout << "Car has stopped" << std::endl;
    }
};
 
int main()
{
    // Using the Facade to start and stop the car
    Car car;
    car.StartCar();
    // Simulate some driving
    car.StopCar();
    return 0;
}

Output

Engine started
Lights on
Car is ready to drive
Lights off
Engine stopped
Car has stopped


Explanation of the above code:

This way, the Facade Pattern hides the complexity of the underlying subsystems (engine and lights) and provides a cleaner and more straightforward interface for clients (the Car class). Clients can interact with the Car class without needing to understand the internal workings of the car components, promoting simplicity and maintainability in the code.

Diagram of the Facade Pattern in C++ Design Pattern:

Flow diagram of facade pattern in c++

In this diagram:

  1. Client represents the class or component that uses the Facade Pattern.
  2. Facade Class is the class that implements the Facade Pattern. It provides a simplified interface to the client and delegates calls to the underlying subsystems.
  3. Subsystem 1 and Subsystem 2 are the components or classes that perform specific operations. The client interacts with these subsystems through the facade.

The Facade Class exposes methods (MethodA, MethodB, MethodC) that orchestrate interactions with the subsystems. Clients interact with the Facade Class and are shielded from the complexities of the subsystems.This diagram visually represents how the Facade Pattern simplifies the interaction between the client and a set of subsystems, making it easier for clients to use the system.

Key Benefits of using the Facade Design Pattern in C++ Design Pattern

Advantages of the Facade Pattern in C++ Design Pattern

Here are the advantages of using the Facade Pattern in C++ ,

Disadvantages of the Facade Pattern in C++ Design Pattern

Here are some of the potential disadvantages or limitations of using the Facade Pattern in C++,

Use Cases of the Facade Pattern in C++ Design Pattern

Here are some common uses of the Facade Pattern in C++:


Article Tags :