设计模式[18]-Proxy

Type: Structural Proxy: 提供一个替代品或者占位符来控制对一个对象的访问。
#include <iostream>  
using namespace std;  

class Subject  
{  
public:  
    virtual void request()=0;  
};   

class RealSubject: public Subject  
{  
public:  
    void request()  
    {  
        cout<<"Real request"<<endl;  
    }  
};  

class Proxy: public Subject  
{  
public:  
    Proxy(RealSubject* pRealSubject):m_pRealSubject(pRealSubject){};  
    void request()  
    {  
        cout<<"Proxy request"<<endl;  
        m_pRealSubject->request();  
    };  
private:  
    RealSubject* m_pRealSubject;  
};  

int main()  
{  
    RealSubject* pRealSubject = new RealSubject;  
    Subject* pProxy = new Proxy(pRealSubject);  
    pProxy->request();  

    system("pause");  
    return 0;  
} 

上一篇:设计模式(十)享元模式Flyweight(结构型)


下一篇:多线程程序设计学习(3)immutable pattern模式