-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapter.cpp
More file actions
172 lines (148 loc) · 4.54 KB
/
Adapter.cpp
File metadata and controls
172 lines (148 loc) · 4.54 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Adapters make legacy code work with modern classes.
// UML: docs/uml/patterns_structural_adapter.drawio.svg
#include <iostream>
namespace AdapterPattern
{
/**
* 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:
static std::string specificRequest()
{
return "Adaptee: The adaptee's behavior.";
}
};
/**
* The Target defines the domain-specific interface used by the client code.
*/
class Target
{
public:
virtual std::string request() const
{
return "Target: The target's behavior.";
}
};
// ============================================================================================================
// [Q] How can we make the clientCode works with Adaptee without change this function (e.g this is in front-end)
// => Create an Adapter
// ============================================================================================================
/**
* The Adapter makes the Adaptee's interface compatible with the Target's
* interface.
*/
class Adapter : public Target
{
private:
Adaptee *m_adaptee;
public:
explicit Adapter(Adaptee *adaptee) : m_adaptee{adaptee}
{
std::cout << "Adapter constructer.\n";
}
std::string request() const override
{
return m_adaptee->specificRequest();
}
};
/**
* The client code supports all classes that follow the Target interface.
*/
namespace Client
{
void clientCode(const Target *target)
{
if (target != nullptr)
std::cout << "Output: " << target->request() << "\n";
}
}
void run()
{
std::cout << "Client: Can work just fine with the Target objects:\n";
Target target = Target();
std::cout << "Target: " << target.request() << "\n";
Client::clientCode(&target);
std::cout << "\n\n";
std::cout << "Client: Cannot work with the Adaptee objects:\n";
Adaptee adaptee = Adaptee();
std::cout << "Adaptee: " << adaptee.specificRequest() << "\n";
// Client::clientCode(&adaptee); // error
std::cout << "Client: But can work with it via the Adapter:\n";
Adapter adapter = Adapter(&adaptee);
Client::clientCode(&adapter);
std::cout << "\n";
}
}
namespace CaseStudy
{
// Target interface expected by the existing system
class PaymentSystem
{
public:
virtual void payWithCard(const std::string &cardNumber)
{
std::cout << "Payment using card: " << cardNumber << "\n";
}
virtual ~PaymentSystem() = default;
};
// Adaptee: a new payment API with an incompatible interface
class PayPalAPI
{
public:
static void sendPayment(const std::string &email)
{
std::cout << "Payment sent via PayPal to " << email << "\n";
}
};
// Adapter: makes PayPalAPI compatible with PaymentSystem
class PayPalAdapter : public PaymentSystem
{
private:
PayPalAPI paypal;
public:
void payWithCard(const std::string &cardNumber) override
{
// Treat the cardNumber parameter as a PayPal email
paypal.sendPayment(cardNumber);
}
};
// Client code: uses the old interface without modification
void run()
{
std::string method, input;
method = std::string("card") + std::string("");
input = "1234-5678-9999";
// method = std::string("paypal") + std::string("");input = "user@example.com";
std::cout << "Choose payment method (card/paypal): " << method << "\n";
PaymentSystem *paymentSystem = nullptr;
if (method == "card")
{
paymentSystem = new PaymentSystem();
paymentSystem->payWithCard(input);
}
else if (method == "paypal")
{
paymentSystem = new PayPalAdapter();
paymentSystem->payWithCard(input);
}
else
{
std::cout << "Unsupported payment method!\n";
}
delete paymentSystem;
}
}
struct AdapterAutoRuner
{
AdapterAutoRuner()
{
std::cout << "\n--- Factory Pattern Example ---\n";
AdapterPattern::run();
CaseStudy::run();
}
};
static AdapterAutoRuner instance;