`
iwindyforest
  • 浏览: 230390 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

代理模式学习笔记

阅读更多

Proxy Pattern

Intent

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

代理模式为另一个对象提供替身或者占位符以控制对这个对象的访问.

 

使用代理模式创建代表(Representative)对象, 让代表对象控制对象的访问, 被代理的对象可以是远程的对象, 创建开销大的对象, 或需要安全控制的对象.

 

代理控制访问的方式:

l  远程代理控制访问远程对象

l  虚拟代理控制访问创建开销大的资源

l  保护代理基于权限控制对资源的访问

 

Applicability

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

 

1.       A remote proxy provides a local representative for a object in a different address space. NEXTSTEP uses the class NXProxy for this purpose. Coplien calls this kind of proxy an “Ambassador”.

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

3.       A protection proxy control access to the original object. Protection proxies are useful when objects should have different access rights. For example, KernelProxies in the choices operating system provide protected access to operating system object.

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

l  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).

l  Loading a persistent object object into memory when it’s first referenced.

l  Checking that real objects is locked before it’s accessed to ensure that no other object can change it.

 

Collaborations

Proxy forwards requests to RealSubject when appropriate, depending on the kind of proxy.

 

Consequences

The proxy pattern introduces a level of indirection when accessing a object. The additional indirection has many uses, depending on the kind of proxy:

1.       A remote proxy can hide the fact that an object resides in a different address space.

2.       A virtual proxy can perform optimizations such as creating a object on demand.

3.       Both protection proxies and smart references allow additional housekeeping tasks when an object is accessed.

Related Patterns

Adapter: An adapter provides a different interface to the object it adapts. In contrast, a proxy provides the same interface as its subject. However, a proxy used for access protection might refuse to perform an operation that the subject will perform, so its interface may be effectively a subset of the subject’s.

Decorator: Although decorators can have similar implementations as proxies, decorators have a different purpose. A decorator adds one or more responsibilities to an object, whereas a proxy controls access to an object. Proxies vary in the degree to which they are implemented like a decorator. A protection proxy might be implemented exactly like a decorator. On the other hand, a remote proxy will not contain a direct reference to its real subject but only an indirect reference, such as ‘host ID and local address on host’. A virtual proxy will start off with an indirect reference such as a file name but will eventually obtain and use a direct reference.

 

代理模式同其它模式的区别和联系

代理                                                                                                     装饰者

控制对象访问                                                                                  装饰对象

只传递请求不改变原来动作          在原来对象进行封装的基础上添加新的责任

远程代理不是封装而是传递请求

虚拟代理会延迟源对象的创建                            源对象的引用必须在封装前存在

 

代理                                                                                                     适配器

代理提供跟源对象同样的接口                            适配器提供了与它适配的对象不同的接口

代理控制对源对象的访问         适配器只是改变适配对象的访问接口, 没有加以控制

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics