c++ (proxy)代理模式

假设我们有几个具有相似的窗体,都包含关闭窗体(closeButton)和按钮单击事件(ClickButton)我们在处理时,不想直接操作每个窗体,可以请求代理。

#include<iostream>
using namespace std;

class IWindow
{
public:
    IWindow(void)
    {
    
    }
    ~IWindow()
    {

    }
    virtual void CloseButton() = 0;
    virtual void ButtonClick() = 0;
private:
};

class ProxyWindow:public IWindow
{
public:
    ProxyWindow(IWindow* win):proxyptr(win)
    {
        
    }
    ~ProxyWindow()
    {

    }
    void CloseButton()
    {
        proxyptr->CloseButton();
    }
    void ButtonClick()
    {
        proxyptr->ButtonClick();
    }
private:
     IWindow* proxyptr;

};

class AWindow:public IWindow
{
public:
    AWindow(void)
    {
    
    }
    ~AWindow()
    {

    }
    void CloseButton()
    {
        std::cout << "A's closeButton" << std::endl;
    }
    void ButtonClick()
    {
        std::cout << "A's ButtonClick" << std::endl;
    }
private:
};
class BWindow:public IWindow
{
public:
    BWindow(void)
    {
    
    }
    ~BWindow()
    {

    }
    void CloseButton()
    {
        std::cout << "B's closeButton" << std::endl;
    }
    void ButtonClick()
    {
        std::cout << "B's ButtonClick" << std::endl;
    }
private:
};

main函数

int main()
{
    ProxyWindow *ptr = new ProxyWindow(new AWindow());
    ptr->ButtonClick();
    ptr->CloseButton();
    
    ProxyWindow *ptr2 = new ProxyWindow(new BWindow());
    ptr2->ButtonClick();
    ptr2->CloseButton();

    getchar();

    return 0;
}

输出结果:

A's ButtonClick
A's closeButton
B's ButtonClick
B's closeButton

 

 

posted @ 2013-12-04 14:55  onlycxue  阅读(1360)  评论(0编辑  收藏  举报