diff --git a/adapter/ObjectAdapter.cpp b/adapter/ObjectAdapter.cpp new file mode 100644 index 0000000..7a6e274 --- /dev/null +++ b/adapter/ObjectAdapter.cpp @@ -0,0 +1,69 @@ +/* + * C++ Design Patterns: Adapter (Object 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 + * defines an existing interface that needs adapting and thanks + * to Adapter it will get calls that client makes on the Target + * + */ +class Adaptee { +public: + void specificRequest() { + std::cout << "specific request" << std::endl; + // ... + } + // ... +}; + +/* + * Adapter + * implements the Target interface and when it gets a method call it + * delegates the call to a Adaptee + */ +class Adapter : public Target { +public: + Adapter() : adaptee() {} + + ~Adapter() { + delete adaptee; + } + + void request() { + adaptee->specificRequest(); + // ... + } + // ... + +private: + Adaptee *adaptee; + // ... +}; + + +int main() +{ + Target *t = new Adapter(); + t->request(); + + return 0; +} diff --git a/adapter/README.md b/adapter/README.md index 339e0f0..fedec24 100644 --- a/adapter/README.md +++ b/adapter/README.md @@ -1,8 +1,9 @@ ## Adapter -Convert the interface of a class into another interface clients expect. +Convert the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of -incompatible interfaces. +incompatible interfaces, ie. allows to use a client with an incompatible +interface by an Adapter that does the conversion. ### When to use