笔记:Gof设计模式--Proxy

1、意图

  Provide a surrogate or placeholder for another object to control access to it.

2、适应性

  Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer. Here are several common situations

  •  A remote proxy provides a local representative for an object in a different address space. NEXTSTEP [Add94] uses the class NXProxy for this purpose. Coplien [Cop92] calls this kind of proxy an "Ambassador."

  •  A virtual proxy creates expensive objects on demand. The ImageProxy described in the Motivation is an example of such a proxy. 

  •  A protection proxy controls access to the original object. Protection proxies are useful when objects should have different access rights. For example, KernelProxies in the Choices operating system [CIRM93] provide protected access to operating system objects.

  •  A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed. Typical uses include

     (1) counting the number of references to the real object so that it can be freed automatically when there are no more references (also called smart pointers [Ede92]).

    (2) loading a persistent object into memory when it's first referenced. 
    
(3) checking that the real object is locked before it's accessed to ensure that no other object can change it.

 

3、结构

 


4、示例代码

 A virtual proxy. The Graphic class defines the interface for graphical objects:

class Graphic { 
    public: 
        virtual ~Graphic(); 
     
        virtual void Draw(const Point& at) = 0; 
        virtual void HandleMouse(Event& event) = 0; 
     
        virtual const Point& GetExtent() = 0; 
     
        virtual void Load(istream& from) = 0; 
        virtual void Save(ostream& to) = 0; 
    protected: 
        Graphic(); 
    }; 


  The Image class implements the Graphic interface to display image files.Image overrides HandleMouse to let users resize the image interactively.

 class Image : public Graphic { 
    public: 
        Image(const char* file);  // loads image from a file 
        virtual ~Image(); 
     
        virtual void Draw(const Point& at); 
        virtual void HandleMouse(Event& event); 
     
        virtual const Point& GetExtent(); 
     
        virtual void Load(istream& from); 
        virtual void Save(ostream& to); 
    private: 
        // ... 
    }; 

 // ImageProxy has the same interface as Image: 
 class ImageProxy : public Graphic { 
    public: 
        ImageProxy(const char* imageFile); 
        virtual ~ImageProxy(); 
     
        virtual void Draw(const Point& at); 
        virtual void HandleMouse(Event& event); 
     
        virtual const Point& GetExtent(); 
     
        virtual void Load(istream& from); 
        virtual void Save(ostream& to); 
    protected: 
        Image* GetImage(); 
    private: 
        Image* _image; 
        Point _extent; 
        char* _fileName; 
    }; 


 ImageProxy::ImageProxy (const char* fileName)  { 
        _fileName = strdup(fileName); 
        _extent = Point::Zero;  // don't know extent yet 
        _image = 0; 
    } 
     
    Image* ImageProxy::GetImage() { 
        if (_image == 0) { 
            _image = new Image(_fileName); 
        } 
        return _image; 
    } 

 const Point& ImageProxy::GetExtent () { 
        if (_extent == Point::Zero) { 
            _extent = GetImage()->GetExtent(); 
        } 
        return _extent; 
    } 
     
    void ImageProxy::Draw (const Point& at) { 
        GetImage()->Draw(at); 
    } 
     
    void ImageProxy::HandleMouse (Event& event) { 
        GetImage()->HandleMouse(event); 
    }


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值