笔记: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:
The Image class implements the Graphic interface to display image files.Image overrides HandleMouse to let users resize the image interactively.
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); }
相关文章推荐
- 设计模式--代理模式Proxy(学习笔记)
- 设计模式C++学习笔记之二(Proxy代理模式)
- 设计模式学习笔记--代理(Proxy)模式
- [学习笔记]设计模式之Proxy
- GOF设计模式学习笔记 --辨析各种模式的要点和相似模式之间的区别
- 笔记:Gof设计模式--Chain of Responsibility
- 【转载】 笔记--设计模式精解c++-GoF 23 种设计模式解析
- 设计模式学习笔记—代理模式(Proxy)
- 笔记:Gof设计模式--State
- 设计模式C++学习笔记之二(Proxy代理模式)
- 设计模式 笔记 代理模式 Proxy
- 笔记--设计模式精解c++-GoF 23 种设计模式解析
- 设计模式学习笔记(十六)—Proxy模式
- 笔记:Gof设计模式--Decorator
- 笔记:Gof设计模式--Observer
- 设计模式(c++)笔记之十二(Proxy模式)
- 【设计模式学习笔记十三】【结构型模式】【代理模式(Proxy)】
- 设计模式学习笔记(十三)——Proxy代理模式
- 设计模式 笔记 代理模式 Proxy
- GOF设计模式学习笔记 --辨析各种模式的要点和相似模式之间的区别