-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy.cpp
44 lines (40 loc) · 1.07 KB
/
strategy.cpp
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
#include <iostream>
#include <string>
#include <algorithm>
class PrintStrategy {
public:
virtual void print(std::string) = 0;
};
class Printer {
public:
Printer(PrintStrategy *strategy): strategy(strategy) {}
~Printer() { delete strategy; }
void print(std::string message) {
strategy->print(std::move(message));
}
private:
PrintStrategy *strategy;
};
class UpperCaseStrategy: public PrintStrategy {
public:
void print(std::string message) override {
std::transform(message.begin(), message.end(), message.begin(),
[](unsigned char c) { return std::toupper(c); });
std::cout << message << std::endl;
}
};
class LowerCaseStrategy: public PrintStrategy {
public:
void print(std::string message) override {
std::transform(message.begin(), message.end(), message.begin(),
[](unsigned char c) { return std::tolower(c); });
std::cout << message << std::endl;
}
};
int main() {
auto lower = Printer(new LowerCaseStrategy());
lower.print("HELLO world");
auto upper = Printer(new UpperCaseStrategy);
upper.print("some STUFF");
return 0;
}