mirror of
https://github.com/JakubVojvoda/design-patterns-cpp.git
synced 2025-12-17 12:54:36 +03:00
add Class Adapter
This commit is contained in:
@@ -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 <iostream>
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
59
adapter/ClassAdapter.cpp
Normal file
59
adapter/ClassAdapter.cpp
Normal file
@@ -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 <iostream>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user