Created
January 1, 2018 20:15
-
-
Save FONQRI/b782fe0149f88802cee3d0de7ffb1a70 to your computer and use it in GitHub Desktop.
Facade Design Pattern in c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <memory> | |
using namespace std; | |
// Transfer library | |
class Usb { | |
public: | |
bool isAvailable(); | |
void connect(); | |
void send(string file); | |
}; | |
bool Usb::isAvailable() { return false; } | |
void Usb::connect() { cout << "Connecting via USB" << endl; } | |
void Usb::send(string file) { cout << file << " sent." << endl; } | |
class Bluetooth { | |
public: | |
bool isAvailable(); | |
void connect(); | |
void authenticate(); | |
void send(string file); | |
}; | |
bool Bluetooth::isAvailable() { return true; } | |
void Bluetooth::connect() { cout << "Connecting via BT" << endl; } | |
void Bluetooth::authenticate() { cout << "Authenticating BT" << endl; } | |
void Bluetooth::send(string file) { cout << file << " sent." << endl; } | |
// The Facade | |
class FileTransfer { | |
public: | |
void sendFile(string fileName); | |
}; | |
void FileTransfer::sendFile(string fileName) | |
{ | |
shared_ptr<Usb> u(new Usb()); | |
shared_ptr<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; | |
} | |
} | |
// Test Program | |
int main() | |
{ | |
unique_ptr<FileTransfer> ft(new FileTransfer()); | |
ft->sendFile("mypicture"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment