代理模式定义:为其他对象提供一种代理以控制对这个对象的访问。在某些去情况下,一个对象不适合或不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介作用。
#include<iostream>
#include<string>
using namespace std;
class Commit
{
public:
virtual void run() = 0;
};
class System:public Commit
{
public:
virtual void run()
{
cout << "系统启动..." << endl;
}
};
class Agency :public Commit//不是所有人都可以启动我的系统,要提高用户名和密码
{
public:
Agency(string username,string userpassword)
{
this->Username = username;
this->Userpassword = userpassword;
system = new System;
}
bool Check()
{
if (Username == "User" && Userpassword == "user")
{
return true;
}
return false;
}
virtual void run()
{
if (Check())
{
cout << "验证成功!" << endl;
this->system->run();
}
else
{
cout << "用户名或者密码错误,验证失败!" << endl;
}
}
~Agency()
{
if (system!=NULL)
{
delete system;
}
}
private:
string Username;
string Userpassword;
System* system;
};
void main()
{
string a, b;
cout << "请输入用户名:" << endl;
cin >> a;
cout << "请输入密码:" << endl;
cin >> b;
Agency* agency = new Agency(a, b);
agency->run();
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/85eaaaaa5f2c44e277c016a4c24fb9bb.png)