- プロキシモード:1つのクラスは別のクラスの機能を表し、他のオブジェクトがこのオブジェクトへのアクセスを制御するためのプロキシを提供します。
- 今
直接访问对象时带来的问题
、例えば:アクセスするオブジェクトは、リモートマシン上にあります。オブジェクト指向システムでは、特定の理由(たとえば、オブジェクトの作成に費用がかかる、特定の操作にセキュリティ制御が必要、またはアウトプロセスアクセスが必要)のために、直接アクセスはユーザーまたはシステムに多くの問題をもたらします。構造。このオブジェクトにアクセスするときに、このオブジェクトにアクセスレイヤーを追加できます。
#include<iostream>
#include<string>
using namespace std;
class AbstractCommonInterface
{
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->m_UserName=username;
this->m_Password=password;
proxy_MySystem=new MySystem;
}
bool check_UsernameAndPassword()
{
if(m_UserName=="admin"&& m_Password=="admin")
{
return true;
}
return false;
}
virtual void run()
{
if(check_UsernameAndPassword())
{
cout<<"验证成功!"<<endl;
this->proxy_MySystem->run();
}
else
{
cout<<"用户名或密码错误!"<<endl;
}
}
~MySystemProxy()
{
if(proxy_MySystem!=NULL)
{
delete proxy_MySystem;
proxy_MySystem=NULL;
}
}
public:
MySystem* proxy_MySystem;
string m_UserName;
string m_Password;
};
void test()
{
MySystemProxy* systemProxy1=new MySystemProxy("root","123");
systemProxy1->run();
MySystemProxy* systemProxy2=new MySystemProxy("admin","admin");
systemProxy2->run();
}
int main()
{
test();
system("pause");
return 0;
}