From af3869d566d442d89f731782f7201fc8dce7a078 Mon Sep 17 00:00:00 2001 From: Jakub Vojvoda Date: Wed, 14 Sep 2016 23:09:27 +0200 Subject: [PATCH] add Class Adapter --- adapter/Adapter.cpp | 17 ------------ adapter/ClassAdapter.cpp | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 17 deletions(-) delete mode 100644 adapter/Adapter.cpp create mode 100644 adapter/ClassAdapter.cpp diff --git a/adapter/Adapter.cpp b/adapter/Adapter.cpp deleted file mode 100644 index 1874a53..0000000 --- a/adapter/Adapter.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/* - * C++ Design Patterns: Adapter - * Author: Jakub Vojvoda [github.com/JakubVojvoda] - * 2016 - * - * Source code is licensed under MIT licence - * (for more details see LICENCE) - * - */ - -#include - -int main() -{ - - return 0; -} \ No newline at end of file diff --git a/adapter/ClassAdapter.cpp b/adapter/ClassAdapter.cpp new file mode 100644 index 0000000..95265ad --- /dev/null +++ b/adapter/ClassAdapter.cpp @@ -0,0 +1,59 @@ +/* + * C++ Design Patterns: Adapter (Class scope) + * Author: Jakub Vojvoda [github.com/JakubVojvoda] + * 2016 + * + * Source code is licensed under MIT licence + * (for more details see LICENCE) + * + */ + +#include + +/* + * Target + * defines specific interface that Client uses + */ +class Target { +public: + virtual void request() = 0; + // ... +}; + +/* + * Adaptee + * all requests get delegated to the Adaptee which defines + * an existing interface that needs adapting + */ +class Adaptee { +public: + void specificRequest() { + std::cout << "specific request" << std::endl; + // ... + } + // ... +}; + +/* + * Adapter + * implements the Target interface and lets the Adaptee respond + * to request on a Target by extending both classes + * ie adapts the interface of Adaptee to the Target interface + */ +class Adapter : public Target, private Adaptee { +public: + virtual void request() { + specificRequest(); + // ... + } + // ... +}; + + +int main() +{ + Target *t = new Adapter(); + t->request(); + + return 0; +}