代理模式(c++)

#include<iostream>
#include<string>
using namespace std;

class AbstractCommonInterface {
public:
	virtual void run() = 0;
};
class MySystem :public AbstractCommonInterface {
public:
	void run()override {
		cout << "程序启动..." << endl;
	}
};
//权限认证
class MySystemProxy :public AbstractCommonInterface {
public:
	MySystemProxy(string name, string pwd) {
		m_name = name;
		m_pwd = pwd;
		pSystem = new MySystem;
	}

	void run() {
		if (checkUsernameAndPassword()) {
			cout << "密码正确,验证通过" << endl;
			pSystem->run();
		}
		else {
			cout << "验证不通过,权限不足" << endl;
		}
	}
private:
	string m_name;
	string m_pwd;
	MySystem* pSystem;

	bool checkUsernameAndPassword() {
		if (m_name == "admin" &&  m_pwd == "admin")return true;
		else return false;
	}
};

int main() {
	MySystemProxy* ms = new MySystemProxy("admin","1");
	ms->run();
	delete ms;

	cin.get();
	return 0;
}

  

posted @ 2024-03-25 17:49  dmfsimle  阅读(3)  评论(0编辑  收藏  举报