-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample.cpp
More file actions
120 lines (94 loc) · 3.08 KB
/
ConceptualExample.cpp
File metadata and controls
120 lines (94 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// ===========================================================================
// ConceptualExample.cpp - Adapter Pattern
// ===========================================================================
#include <iostream>
#include <string>
#include <memory>
#include <algorithm>
/**
* ITarget defines the domain-specific interface used by the client code
*/
class ITarget
{
public:
virtual ~ITarget() {}
virtual std::string request() const = 0;
};
/**
* Target implements ITarget - implementation usable by the client code
*/
class Target : public ITarget
{
public:
Target() {}
std::string request() const override
{
return std::string{ "Target: The target's default behavior." };
}
};
/**
* The Adaptee contains some useful behavior,
* but its interface is incompatible with the existing client code.
* The Adaptee needs some adaptation before the client code can use it.
*/
class Adaptee
{
public:
Adaptee() {}
std::string specificRequest() const
{
return std::string{ ".eetpadA eht fo roivaheb laicepS" };
}
};
/**
* The Adapter makes the Adaptee's interface
* compatible with the ITarget's interface.
*/
class Adapter : public ITarget
{
private:
std::unique_ptr<Adaptee> m_adaptee;
public:
Adapter(std::unique_ptr<Adaptee> adaptee)
: m_adaptee{ std::move(adaptee) }
{}
std::string request() const override {
std::string toReverse{ m_adaptee->specificRequest() };
std::reverse(
toReverse.begin(),
toReverse.end()
);
return std::string{ "Adapter: (TRANSLATED) " + toReverse };
}
};
/**
* The client code supports all classes that follow the Target interface
*/
static void clientCode(std::unique_ptr<ITarget> target) {
std::string response{ target->request() };
std::cout << response << std::endl << std::endl;
}
static void test_conceptual_example_01() {
std::unique_ptr<Adaptee> adaptee{ std::make_unique<Adaptee>() };
std::unique_ptr<Adapter> adapter{ std::make_unique<Adapter>(std::move(adaptee)) };
clientCode(std::move(adapter));
}
static void test_conceptual_example_02() {
std::cout << "Client: I can work fine with the Target object" << std::endl;
std::unique_ptr<ITarget> target{ std::make_unique<Target>() };
clientCode(std::move(target));
std::unique_ptr<Adaptee> adaptee{ std::make_unique<Adaptee>() };
std::cout << "Client: The Adaptee class has an incompatible interface:" << std::endl;
std::string specificResponse = adaptee->specificRequest();
std::cout << "Adaptee: " << specificResponse << std::endl << std::endl;
std::cout << "Client: But I can work with the Adaptee via the Adapter:" << std::endl;
std::unique_ptr<Adapter> adapter{ std::make_unique<Adapter>(std::move(adaptee)) };
clientCode(std::move(adapter));
}
void test_conceptual_example() {
test_conceptual_example_01();
test_conceptual_example_02();
}
// ===========================================================================
// End-of-File
// ===========================================================================