-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.h
More file actions
46 lines (38 loc) · 849 Bytes
/
Copy pathproxy.h
File metadata and controls
46 lines (38 loc) · 849 Bytes
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
// # Copyright 2022 CalvinHxx. All rights reserved.
#ifndef SRC_STRUCTURAL_PATTERNS_PROXY_H_
#define SRC_STRUCTURAL_PATTERNS_PROXY_H_
#include <iostream>
namespace Proxy {
class Graphic {
public:
virtual void Display() = 0;
};
class Image : public Graphic {
public:
void Display() override { std::cout << "Image" << std::endl; }
};
class Proxy : public Graphic {
public:
void Display() override { ImageProxy().Display(); }
protected:
Image& ImageProxy() {
if (!p_) {
p_ = new Image;
}
return *p_;
}
private:
Image* p_;
};
void Client() {
std::cout << "***"
<< "Proxy"
<< "***\n";
Graphic* image_proxy = new Proxy();
image_proxy->Display();
std::cout << "***"
<< "Proxy"
<< "***\n";
}
} // namespace Proxy
#endif // SRC_STRUCTURAL_PATTERNS_PROXY_H_