美文网首页C++设计模式
【C++设计模式】代理模式

【C++设计模式】代理模式

作者: 小鱼号的代码日记 | 来源:发表于2021-02-27 10:25 被阅读0次
/*设计模式
 *  代理模式
 *  提供一种代理来控制对其他对象的访问
*/
#include<iostream>
using namespace std;

class AbstractCommonInterface
{
    virtual void run() = 0;
};

class MySystem :public AbstractCommonInterface
{
public:
    virtual void run()
    {
        cout << "system run" << endl;
    }
};

//必须要有权限验证,不是所有人都能来启动系统,需要提供用户名和密码

class MySystemProxy :public AbstractCommonInterface
{
public:
    MySystemProxy(string userName,string password)
    {
        mUserName = userName;
        mPassword = password;
        pSystem = new MySystem;
    }
    virtual void run()
    {
        cout << "proxy run" << endl;
        if(checkUserNameAndPassword())
            pSystem->run();
        else
            cout << "system check error" << endl;
    }
    ~MySystemProxy()
    {
       if(pSystem != nullptr)
           delete pSystem;
    }
private:
    bool checkUserNameAndPassword()
    {
        return true;
    }
public:
    MySystem *pSystem;
    string mUserName;
    string mPassword;
};

void testProxy()
{
    MySystemProxy proxy("user","pwd");
    proxy.run();
}

相关文章

网友评论

    本文标题:【C++设计模式】代理模式

    本文链接:https://www.haomeiwen.com/subject/uabbfltx.html