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.
代理模式同其它模式的区别和联系
代理 装饰者
控制对象访问 装饰对象
只传递请求不改变原来动作 在原来对象进行封装的基础上添加新的责任
远程代理不是封装而是传递请求
虚拟代理会延迟源对象的创建 源对象的引用必须在封装前存在
代理 适配器
代理提供跟源对象同样的接口 适配器提供了与它适配的对象不同的接口
代理控制对源对象的访问 适配器只是改变适配对象的访问接口, 没有加以控制
分享到:
相关推荐
代理模式学习笔记、单例模式学习笔记、命令模式、原型模式、模式特点总结。 为个人对设计模式的理解,如果有理解不一致的不要砸砖啊
代理设计模式、命令模式学习笔记,其中包含对模式的理解以及详细的模式使用示例
16-Java代理模式的学习笔记1
《C++20设计模式》学习笔记-第12章代理模式-配套代码
工厂模式,策略模式,装饰模式,代理模式,工厂方法模式,原型模式等23个设计模式
结构型模式:适配器模式(3种实现)、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式(3种实现)。行为型模式:模版方法模式、命令模式、访问者模式、迭代器模式、观察者模式、中介者模式、备忘录模式、...
JavaScript常见的一些设计模式学习笔记和代码实现。包括工厂模式、单例模式、代理模式、发布订阅模式、中介者模式等等内容。
该文档是自己在学习设计模式时整理的常用设计模式pdf文档,包括源码,包括装饰模式,代理模式,责任链模式,命令模式,解释器模式,迭代器模式,备忘录模式,观察者模式,工厂模式,建造者模式,适配器模式,桥梁...
NULL 博文链接:https://coolszy.iteye.com/blog/523111
NULL 博文链接:https://coolszy.iteye.com/blog/523105
2. 结构型模式:结构型模式关注对象之间的组合关系,包括适配器模式、桥接模式、组合模式、装饰器模式、外观模式、享元模式和代理模式。这些模式可以帮助我们更好地组织和管理对象之间的关系,提高代码的灵活性和可...
Java多线程 基础学习笔记 / 学习静态代理模式-附件资源
SpringAOP学习笔记以及四个可运行的Demo,涵盖经典代理模式、基于注解、基于xml配置这3方面的Demo
JAVA,设计模式动态代理,学习笔记。
针对几种经典软件设计模式:模板方法模式 外观模式 代理模式 装饰器模式 桥模式 工厂方法模式 组合模式等给出了简单易懂的讲解,透彻明了。很好的学习资料哦,是我平时学习时做的笔记外加自己的理解,很不错的……
学习笔记较长,如果想查看文章的目录,chrome浏览器可以配合简悦插件使用,或者下载到本地使用Typora打开。 :hot_beverage: Java基础 Java容器 Java并发 数据结构和算法 数据结构 树 图 算法 分治 动态规划 回溯 ...
{7.2}回调模式与FileFilter}{104}{section.7.2} {7.3}\ttfamily RandomAccessFile}{106}{section.7.3} {7.4}基本类型数据序列化}{108}{section.7.4} {7.5}String的序列化}{109}{section.7.5} {7.6}InputStream...
代理模式 外观模式 组合模式 装饰模式 享元模式 桥接模式 Builder模式 状态模式 解释器模式 观察者模式 备忘录模式 迭代器模式 访问者模式 中介者模式 第二部分:JAVA 第三部分:Spring框架 第四部分:中间件 第五...