代理模式
- 为其他对象提供一种代理以控制这个对象的访问
- 在某些条件下一个对象不适合直接引用另外一个对象
- 代理模式对象可以做一个中介作用
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;
this->pSystem = new MySystem();
}
bool checkUsernameAndPassword()
{
if ((this->mUsername == "admin" )&&( this->mPassword == "admin"))
{
return true;
}
return false;
}
virtual void run()
{
if (checkUsernameAndPassword())
{
this->pSystem->run();
}
else
{
cout << "权限不足" << endl;
}
}
~MySystemProxy()
{
if (this->pSystem != nullptr)
{
delete this->pSystem;
}
}
public:
MySystem* pSystem;
string mUsername;
string mPassword;
};
void test()
{
MySystemProxy* proxy = new MySystemProxy("admin1", "admin");
proxy->run();
return;
}