The facade design pattern in C++ with real life example

As we know, there are three types of design pattern but you need to know that a facade design pattern is a structural design pattern

Intent

Gives an interface that is easier to use subsystems and provides an interface for a collection of subsystem interface

Core concept

Facade design pattern reduces the complexity by structuring  system into a subsystem and limits the dependencies, communications between subsystems

Example

Suppose you have a software called concert and it contains three subsystems and each subsystem have different functions

Further more if a client want to play a concert he doesn’t need to call each function to play a concert

Participants

  • Facade(concert)
  • Subsystem classes(guitar_subsystem1, vocals_subsystem2, drums_subsystem3)
#include <iostream>
using namespace std;
class Guitar_Subsystem1
{
public:
  void guitarcable()   { cout << " Plug in electric guitar cable in the switch \n";}
  void adjustment(){ cout << " String adjustment of the guitar\n";}
  void amplifier (){ cout << " Connect amplifier for audio quality\n";}
};
class Vocals_Subsystem2
{
public:
    void Vocalartist (){ cout << " Vocal artist for a concert\n";}
    void microphone() { cout << " Set microphone with stand\n";}
    void Pluginmicrophone () { cout << " Plugin microphone in switch\n";}
};
class Drums_Subsystem3
{
public:
    void drumstick (){ cout << " Get two drum sticks\n";}
    void drumsstand() { cout << " Set drums stand\n";}
    void drummerchair () { cout << " Set chair for a drummer \n";}
};
class concert_Facade
{
private:
    Guitar_Subsystem1 guitar;
    Vocals_Subsystem2 vocals;
    Drums_Subsystem3 drums;
public:
    void Playconcert()
    {
        guitar.guitarcable();
        guitar.adjustment();
        guitar.amplifier();
         vocals.Vocalartist();
         vocals.microphone();
         vocals.Pluginmicrophone();
         drums.drumstick ();
         drums.drumsstand();
         drums.drummerchair();
    }
};
int main()
{
    concert_Facade facade;
    facade.Playconcert();
return 0;
}

Output

Plug in electric guitar cable in the switch
string adjustment of the guitar
connect amplifier for audio quality
vocal artist for a concert
set microphone with stand
plug in microphone in switch
get two drum sticks
set drums stand
set chair for a drummer

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.