Implementation of the Facade Pattern in C++ Design Pattern

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

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:

  • We have two subsystems, Engine and Lights, each with its own methods.
  • The Car class acts as the facade. It encapsulates the interactions with the subsystems and provides a simplified interface to the client.
  • The Car class’s StartCar and StopCar methods orchestrate the interactions with the subsystems to start and stop the car. Clients don’t need to know the details of how the engine or lights work, they simply call the methods on the Car facade.

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.

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.

Important Topics for the Facade Method in C++ Design Patterns

  • Implementation of the Facade Pattern in C++ Design Pattern
  • Diagram of the Facade Pattern in C++ Design Pattern:
  • Key Benefits of using the Facade Design Pattern in C++ Design Pattern
  • Advantages of the Facade Pattern in C++ Design Pattern
  • Disadvantages of the Facade Pattern in C++ Design Pattern
  • Use Cases of the Facade Pattern in C++ Design Pattern

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.

Similar Reads

Implementation of the Facade Pattern in C++ Design Pattern

...

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

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

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

...

Advantages of the Facade Pattern in C++ Design Pattern

Flow diagram of facade pattern in c++...

Disadvantages of the Facade Pattern in C++ Design Pattern

Simplified Interface: Clients only need to know about the facade’s methods and don’t need to understand the complexities of the underlying subsystems. Reduced Complexity: The pattern hides the intricacies of the subsystems, making the system easier to comprehend and manage. Loose Coupling: Clients are decoupled from the subsystems, which allows for more flexible and modular software design. Easy Maintenance: Changes to the subsystems can be isolated within the facade, minimizing the impact on client code. Improved Testing: Testing becomes easier since clients interact with a single interface rather than multiple subsystems....

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

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

Contact Us