您的位置:首页 > 编程语言 > Go语言

笔记:Gof设计模式--Proxy

2012-06-02 18:00 363 查看
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、结构



[align=left] [/align]

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: