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
| typedef enum class ePERMIT_T
{
ALLOW,
DISALLOW
} ePERMIT;
class Proxy :
public Subject
{
public:
Proxy(ePERMIT permit)
{
m_permit = permit;
m_count = 0;
}
void action1() override
{
std::cout << "[Proxy] action1 doing" << std::endl;
m_count++;
}
void action2() override
{
if (m_permit == ePERMIT::ALLOW)
{
// 게으른 객체 생성
if (m_realSubject == nullptr)
{
real();
}
std::cout << "[Proxy] action2 doing" << std::endl;
m_realSubject->action2();
m_count++;
}
else
{
std::cout << "[Proxy] disallow action2" << std::endl;
}
}
// 스마트 프록시
int getCount()
{
return m_count;
}
private:
void real()
{
std::cout << "[Proxy] concrete RealSubject object" << std::endl;
m_realSubject = std::make_shared<RealSubject>();
}
std::shared_ptr<RealSubject> m_realSubject;
ePERMIT m_permit;
int m_count;
};
|