Facade Pattern

What is Facade Pattern?

  • Facade pattern is a structural design pattern.
  • Makes an existing complex software library easier to use by providing a simpler interface for common tasks.
  • Allows the applications/ clients using the library de-coupled from inner workings of a complex library.

Demonstrate a simple Facade Pattern in C++

#include <iostream>using namespace std;// Transfer libraryclass Usb {public:bool isAvailable(){return false;}void connect(){cout << "Connecting via USB" << endl;}void send(string file){cout << file << " sent." << endl;}};class Bluetooth {public:bool isAvailable(){return true;}void connect(){cout << "Connecting via BT" << endl;}void authenticate(){cout << "Authenticating BT" << endl;}void send(string file){cout << file << " sent." << endl;}};// The Facadeclass FileTransfer {public:void sendFile(string fileName){Usb* u = new Usb();Bluetooth* b = new Bluetooth();if ( u->isAvailable() ){u->connect();u->send(fileName);}else if ( b->isAvailable() ){b->connect();b->authenticate();b->send(fileName);}else{cout << "Not sent" << endl;}delete b;delete u;}};// Test Programint main(){FileTransfer* ft = new FileTransfer();ft->sendFile("mypicture");delete ft;}

OUTPUT:-

Connecting via BTAuthenticating BTmypicture sent.