-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.cc
64 lines (52 loc) · 1.22 KB
/
proxy.cc
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
//
// Design pattern # proxy
// Flyweight pattern is used to reduce the number of
// objects created.
//
// g++ -std=c++17 -Wall -Wextra -o proxy proxy.cc
//
#include <iostream>
struct Executer {
virtual bool execute(const std::string &command) = 0;
};
struct User: Executer {
bool execute(const std::string &command) override {
std::cout << "Execute command # " << command << '\n';
return true;
}
};
// Proxy user
struct Root: Executer {
std::string password;
Root(const std::string &password) {
this->password = password;
}
bool execute(const std::string &command) override {
if (authenicate())
std::cout << "Execute command # " << command << '\n';
return false;
}
bool authenicate() {
return this->password == "root";
}
};
//
// Entry function
//
int main() {
std::cout << "Design pattern # proxy\n";
bool ret;
User user;
ret = user.execute("show me the contents");
if (!ret)
std::cout << "Fail to execute command\n";
Root root1("user");
ret = root1.execute("move contents");
if (!ret)
std::cout << "Fail to execute command\n";
Root root2("root");
ret = root2.execute("move contents");
if (!ret)
std::cout << "Fail to execute command\n";
return 0;
}