代理模式
没有代理的代码
#include <iostream>
using namespace std;
#include <string>
class SchoolGirl {
private:
string name;
public:
void setName(string name) {
this->name = name;
}
string getName() {
return this->name;
}
};
class Pursuit {
public:
Pursuit(SchoolGirl* mm) {
this->mm = mm;
}
void GiveDolls() {
cout << mm->getName() << "送你洋娃娃" << endl;
}
void GiveFlowers() {
cout << mm->getName() << "送你鲜花" << endl;
}
SchoolGirl* mm = NULL;
};
int main() {
SchoolGirl* jiaojiao = new SchoolGirl;
jiaojiao->setName("李娇娇");
Pursuit* zhuojiayi = new Pursuit(jiaojiao); //娇娇并不认识卓贾易,此处有问题
zhuojiayi->GiveDolls();
zhuojiayi->GiveFlowers();
return 0;
}
只有代理的代码
#include <iostream>
using namespace std;
#include <string>
class SchoolGirl {
private:
string name;
public:
void setName(string name) {
this->name = name;
}
string getName() {
return this->name;
}
};
class Proxy {
public:
Proxy(SchoolGirl* mm) {
this->mm = mm;
}
void GiveDolls() {
cout << mm->getName() << "送你洋娃娃" << endl;
}
void GiveFlowers() {
cout << mm->getName() << "送你鲜花" << endl;
}
SchoolGirl* mm = NULL;
};
int main() {
SchoolGirl* jiaojiao = new SchoolGirl;
jiaojiao->setName("李娇娇");
Proxy* daili = new Proxy(jiaojiao);
daili->GiveDolls();
daili->GiveFlowers();
return 0;
}
这里只是把 Pursuit(追求者)换成了 Proxy(代理者),这样就变成了礼物是代理者送的了,并没有将 SchoolGirl(被追求者)、Pursuit(追求者)、Proxy(代理者)三者联系起来。
#include <iostream>
using namespace std;
#include <string>
class SchoolGirl {
private:
string name;
public:
void setName(string name) {
this->name = name;
}
string getName() {
return this->name;
}
};
//代理接口抽象类
class GiveGift {
public:
virtual void GiveDolls() = 0;
virtual void GiveFlowers() = 0;
};
//追求者类
class Pursuit :public GiveGift {
public:
Pursuit(SchoolGirl* mm) {
this->mm = mm;
}
void GiveDolls() {
cout << mm->getName() << "送你洋娃娃" << endl;
}
void GiveFlowers() {
cout << mm->getName() << "送你鲜花" << endl;
}
SchoolGirl* mm = NULL;
};
//代理类
class Proxy :public GiveGift {
public:
Proxy(SchoolGirl* mm) {
gg = new Pursuit(mm);
}
void GiveDolls() {
gg->GiveDolls(); //在实现方法中去调用“追求者”类的相关方法
}
void GiveFlowers() {
gg->GiveFlowers();
}
~Proxy() {
if (gg != NULL) {
delete gg;
}
}
public:
Pursuit* gg;
};
int main() {
SchoolGirl* jiaojiao = new SchoolGirl;
jiaojiao->setName("李娇娇");
Proxy* daili = new Proxy(jiaojiao);
daili->GiveDolls();
daili->GiveFlowers();
return 0;
}
另附博文:c++之代理模式
实例1
#include <iostream>
using namespace std;
//抽象工厂
class IFactory
{
public:
virtual ~IFactory() {}
virtual void createProduct() = 0;
};
//苹果AirPods耳机工厂
class AppleAirpodsFactory :public IFactory
{
public:
void createProduct()
{
cout << "生产苹果AirPods耳机" << endl;
}
};
//立讯精密公司代理生产
class ILXCompanyProxy :public IFactory
{
public:
ILXCompanyProxy() {
m_factory = new AppleAirpodsFactory;
}
void createProduct()
{
cout << "立讯精密代理:" << endl;
m_factory->createProduct();
}
~ILXCompanyProxy() {
if (m_factory != NULL) {
delete m_factory;
}
}
private:
AppleAirpodsFactory* m_factory;
};
//客户端
int main()
{
ILXCompanyProxy* proxy = new ILXCompanyProxy;
proxy->createProduct();
return 0;
}
实例2
#include <iostream>
using namespace std;
#include <string>
class AbstractCommonInterface {
public:
virtual void run() = 0;
};
class MySystem :public AbstractCommonInterface {
public:
virtual void run() {
cout << "系统启动" << endl;
}
};
class MySystemProxy :public AbstractCommonInterface {
public:
MySystemProxy(string username, string password) {
this->mUsername = username;
this->mPassword = password;
pSystem = new MySystem;
}
bool checkUsernameAndPassword() {
if (mUsername == "admin" && mPassword == "admin") {
return true;
}
return false;
}
virtual void run() {
if (checkUsernameAndPassword()) {
cout << "用户名和密码正确,验证通过" << endl;
this->pSystem->run();
}
else {
cout << "用户名或密码错误,权限不足" << endl;
}
}
~MySystemProxy() {
if (pSystem != NULL) {
delete pSystem;
}
}
public:
MySystem* pSystem;
string mUsername;
string mPassword;
};
void test01() {
MySystemProxy* proxy = new MySystemProxy("admin", "admin");
proxy->run();
}
int main() {
test01();
return 0;
}