proxy mode

definition

   Provides a proxy for other objects to control access to this object

main solution

   Problems when accessing objects directly, for example: the object to be accessed is on a remote server. In the object-oriented system, for some reasons, direct access to some objects will bring a lot of trouble to the user or the system, and an access layer to this object can be added when accessing this object.

How to solve

   Add intermediate proxy layer

key code

   Implementing and proxied class composition

class Gril  
{  
public:  
    Gril(string name = "gril"):m_string(name){}  
    string getName()  
    {  
        return m_string;  
    }  
private:  
    string m_string;  
};  
  
class Profession  
{  
public:  
    virtual ~Profession(){}  
    virtual void profess() = 0;  
};  
  
class YoungMan:public Profession  
{  
public:  
    YoungMan (Grill): m_grill {}  
    void profess()  
    {  
        cout << "Young man love " << m_gril.getName().data() << endl;  
    }  
private:  
    Gril m_gril;  
};  
  
class ManProxy:public Profession  
{  
public:  
    ManProxy(Gril gril):m_man(new YoungMan(gril)){}  
    void profess()  
    {  
        cout << "I am Proxy" << endl;  
        m_man->profess();  
    }  
private:  
    YoungMan* m_man;  
};  
  
int main(int argc, char *argv[])  
{  
    Gril gril("hei");  
    Profession* proxy = new ManProxy(gril);  
    proxy->profess();  
    delete proxy;  
    return 0;  
}  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324882197&siteId=291194637