parent
aa498f1390
commit
3614b77b9e
|
@ -5,7 +5,7 @@
|
|||
#include <time.h>
|
||||
using namespace std;
|
||||
|
||||
// 抽象主题角色
|
||||
// 抽象主题角色
|
||||
class Subject
|
||||
{
|
||||
public:
|
||||
|
@ -14,18 +14,18 @@ public:
|
|||
virtual void method() = 0;
|
||||
};
|
||||
|
||||
// 真实主题角色
|
||||
// 真实主题角色
|
||||
class RealSubject :public Subject
|
||||
{
|
||||
public:
|
||||
RealSubject(){}
|
||||
virtual ~RealSubject(){}
|
||||
void method(){
|
||||
printf("调用业务方法\n");
|
||||
printf("调用业务方法\n");
|
||||
}
|
||||
};
|
||||
|
||||
// Log类
|
||||
// Log类
|
||||
class Log
|
||||
{
|
||||
public:
|
||||
|
@ -33,13 +33,13 @@ public:
|
|||
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:
|
||||
|
@ -47,8 +47,14 @@ public:
|
|||
realSubject = new RealSubject();
|
||||
log = new Log();
|
||||
}
|
||||
~Proxy(){
|
||||
delete realSubject;
|
||||
delete log;
|
||||
realSubject = nullptr;
|
||||
log = nullptr;
|
||||
}
|
||||
void preCallMethod(){
|
||||
printf("方法method()被调用,调用时间为%s\n",log->getTime().c_str());
|
||||
printf("方法method()被调用,调用时间为%s\n",log->getTime().c_str());
|
||||
}
|
||||
void method(){
|
||||
preCallMethod();
|
||||
|
@ -56,11 +62,11 @@ public:
|
|||
postCallMethod();
|
||||
}
|
||||
void postCallMethod(){
|
||||
printf("方法method()调用调用成功!\n");
|
||||
printf("方法method()调用调用成功!\n");
|
||||
}
|
||||
private:
|
||||
RealSubject *realSubject;
|
||||
Log* log;
|
||||
};
|
||||
|
||||
#endif //__FLYPATTERN_PATTERN_H__
|
||||
#endif //__FLYPATTERN_PATTERN_H__
|
||||
|
|
Loading…
Reference in New Issue