forked from FengJungle/DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxyPattern.h
More file actions
74 lines (68 loc) · 1.23 KB
/
ProxyPattern.h
File metadata and controls
74 lines (68 loc) · 1.23 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
#ifndef __FLYPATTERN_PATTERN_H__
#define __FLYPATTERN_PATTERN_H__
#include <mutex>
#include <time.h>
using namespace std;
// 抽象主题角色
class Subject
{
public:
Subject(){}
virtual ~Subject(){}
virtual void method() = 0;
};
// 真实主题角色
class RealSubject :public Subject
{
public:
RealSubject(){}
virtual ~RealSubject(){}
void method(){
printf("调用业务方法\n");
}
};
// Log类
class Log
{
public:
Log(){}
string getTime(){
time_t t = time(NULL);
char ch[64] = { 0 };
//年-月-日 时:分:秒
strftime(ch, sizeof(ch)-1, "%Y-%m-%d %H:%M:%S", localtime(&t));
return ch;
}
};
// 代理类
class Proxy:public Subject
{
public:
Proxy(){
realSubject = new RealSubject();
log = new Log();
}
Proxy(const Proxy& o) = delete;
Proxy& operator=(const Proxy&) = delete;
~Proxy(){
delete realSubject;
delete log;
realSubject = nullptr;
log = nullptr;
}
void preCallMethod(){
printf("方法method()被调用,调用时间为%s\n",log->getTime().c_str());
}
void method(){
preCallMethod();
realSubject->method();
postCallMethod();
}
void postCallMethod(){
printf("方法method()调用调用成功!\n");
}
private:
RealSubject *realSubject;
Log* log;
};
#endif //__FLYPATTERN_PATTERN_H__