2019-10-29 14:14:19 +00:00
|
|
|
#ifndef __FLYPATTERN_PATTERN_H__
|
|
|
|
#define __FLYPATTERN_PATTERN_H__
|
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
#include <time.h>
|
|
|
|
using namespace std;
|
|
|
|
|
2021-10-09 05:41:07 +00:00
|
|
|
// 抽象主题角色
|
2019-10-29 14:14:19 +00:00
|
|
|
class Subject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Subject(){}
|
2021-04-04 13:09:08 +00:00
|
|
|
virtual ~Subject(){}
|
2019-10-29 14:14:19 +00:00
|
|
|
virtual void method() = 0;
|
|
|
|
};
|
|
|
|
|
2021-10-09 05:41:07 +00:00
|
|
|
// 真实主题角色
|
2019-10-29 14:14:19 +00:00
|
|
|
class RealSubject :public Subject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RealSubject(){}
|
2021-04-04 13:09:08 +00:00
|
|
|
virtual ~RealSubject(){}
|
2019-10-29 14:14:19 +00:00
|
|
|
void method(){
|
2021-10-09 05:41:07 +00:00
|
|
|
printf("调用业务方法\n");
|
2019-10-29 14:14:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-09 05:41:07 +00:00
|
|
|
// Log类
|
2019-10-29 14:14:19 +00:00
|
|
|
class Log
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Log(){}
|
|
|
|
string getTime(){
|
|
|
|
time_t t = time(NULL);
|
|
|
|
char ch[64] = { 0 };
|
2021-10-09 05:41:07 +00:00
|
|
|
//年-月-日 时:分:秒
|
2019-10-29 14:14:19 +00:00
|
|
|
strftime(ch, sizeof(ch)-1, "%Y-%m-%d %H:%M:%S", localtime(&t));
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-09 05:41:07 +00:00
|
|
|
// 代理类
|
2019-10-29 14:14:19 +00:00
|
|
|
class Proxy:public Subject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Proxy(){
|
|
|
|
realSubject = new RealSubject();
|
|
|
|
log = new Log();
|
|
|
|
}
|
2021-10-09 05:41:07 +00:00
|
|
|
~Proxy(){
|
|
|
|
delete realSubject;
|
|
|
|
delete log;
|
|
|
|
realSubject = nullptr;
|
|
|
|
log = nullptr;
|
|
|
|
}
|
2019-10-29 14:14:19 +00:00
|
|
|
void preCallMethod(){
|
2021-10-09 05:41:07 +00:00
|
|
|
printf("方法method()被调用,调用时间为%s\n",log->getTime().c_str());
|
2019-10-29 14:14:19 +00:00
|
|
|
}
|
|
|
|
void method(){
|
|
|
|
preCallMethod();
|
|
|
|
realSubject->method();
|
|
|
|
postCallMethod();
|
|
|
|
}
|
|
|
|
void postCallMethod(){
|
2021-10-09 05:41:07 +00:00
|
|
|
printf("方法method()调用调用成功!\n");
|
2019-10-29 14:14:19 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
RealSubject *realSubject;
|
|
|
|
Log* log;
|
|
|
|
};
|
|
|
|
|
2021-10-09 05:41:07 +00:00
|
|
|
#endif //__FLYPATTERN_PATTERN_H__
|