代理类就是在调用原本功能的同时去调用一个其他的功能,而不影响原本功能的执行。这样可以实现对原本功能的增强或控制,而不需要修改原本功能的代码
#include <iostream>
// 抽象主题类
class Subject {
public:
virtual ~Subject() = default;
virtual void business() = 0; // 定义业务操作
};
// 真实主题类
class RealSubject : public Subject {
public:
RealSubject() {
std::cout << "RealSubject Hello" << std::endl;
}
~RealSubject() override {
std::cout << "RealSubject Bye" << std::endl;
}
void business() override {
std::cout << "business 业务代码在此执行" << std::endl;
}
};
// 附加功能:日志
class Log {
public:
Log() {
std::cout << "Log Hello" << std::endl;
}
~Log() {
std::cout << "Log Bye" << std::endl;
}
// 获取当前时间并格式化为字符串
std::string addTimeLog() {
time_t t = time(nullptr);
char timeLog[64] = { 0 };
std::strftime(timeLog, 63, "%Y-%m-%d %H:%M:%S", localtime(&t));
return std::string(timeLog);
}
};
// 代理主题类
class ProxySubject : public Subject {
public:
ProxySubject() {
std::cout << "ProxySubject Hello" << std::endl;
realSubject = new RealSubject(); // 创建真实主题对象
log = new Log(); // 创建日志对象
}
~ProxySubject() override {
std::cout << "ProxySubject Bye" << std::endl;
delete realSubject; // 删除真实主题对象
delete log; // 删除日志对象
}
// 在业务操作之前添加日志
void preBusiness() {
std::cout << "preBusiness 被调用,时间为:" << log->addTimeLog() << std::endl;
}
// 调用真实主题对象的业务操作,并在前后添加日志
void business() override {
preBusiness();
realSubject->business();
std::cout << "输入任意键继续:" << std::endl;
getchar();
afterBusiness();
}
// 在业务操作之后添加日志
void afterBusiness() {
std::cout << "afterBusiness 被调用,时间为:" << log->addTimeLog() << std::endl;
}
private:
RealSubject* realSubject; // 真实主题对象的引用
Log* log; // 日志对象的引用
};
// 客户端代码
void ClientCode(const Subject& subject) {
subject.business(); // 调用代理对象的业务操作
}
int main() {
ProxySubject proxy; // 创建代理对象
ClientCode(proxy); // 使用代理对象访问真实主题对象
}
#include <iostream>
using namespace std;
class DogSubject {
protected:
virtual void cry() = 0;
virtual ~DogSubject() = default; // 虚析构函数
};
class BackDog :public DogSubject {
public:
BackDog(string name) : name(name){}
void cry() {
cout << "吃东西" << endl;
}
string Getname() {
return this->name;
}
~BackDog() override { // 虚析构函数
cout << "BackDog Bye" << endl;
}
private:
string name;
};
class Log {
public:
Log() {
}
void StratcryLog(BackDog* backdog) {
cout << backdog->Getname() << "准备吃东西" << endl;
}
void EndcryLog(BackDog* backdog) {
cout << backdog->Getname() << "东西吃完了" << endl;
}
~Log()
{
cout << "Log Bye" << endl;
}
};
class ProxyBackDogSubject : public DogSubject {
public:
ProxyBackDogSubject() {
this->log = new Log();
this->backdog = nullptr; // 初始化为nullptr
}
~ProxyBackDogSubject() override { // 虚析构函数
cout << "ProxyBackDogSubject Bye" << endl;
delete this->backdog; // 释放真实主题对象
delete this->log; // 释放日志对象
}
void cry() {
if (this->backdog == nullptr) return; // 判断真实主题对象是否为空
this->log->StratcryLog(this->backdog);
this->backdog->cry();
this->log->EndcryLog(this->backdog);
}
void SetBackdog(BackDog* backdog) {
this->backdog = backdog;
}
private:
BackDog* backdog;
Log* log;
};
void Client(DogSubject* subject) { // 接收一个抽象主题类的指针
subject->cry();
}
int main() {
DogSubject* subject = new ProxyBackDogSubject(); // 创建一个代理对象
Client(subject); // 使用代理对象访问真实主题对象
delete subject; // 释放代理对象
}