`
iwindyforest
  • 浏览: 233644 次
  • 性别: 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
分享到:
评论

相关推荐

    设计模式学习笔记总结

    这里我们聚焦于C#语言中的设计模式学习笔记,涵盖了多种经典的设计模式,如合成模式、桥梁模式、装饰模式、享元模式、门面模式、命令模式、工厂方法、策略模式、代理模式以及状态模式。下面将对这些模式逐一进行详细...

    23种设计模式学习笔记

    这个“23种设计模式学习笔记”文档将引导你深入理解这些模式,并帮助你在实际编程中有效地应用它们。以下是对23种设计模式的详细解读: 1. **单例模式**:确保一个类只有一个实例,并提供全局访问点。它常用于控制...

    HeadFirst设计模式学习笔记

    《HeadFirst设计模式学习笔记》是一份详尽的资料,旨在帮助读者深入理解并掌握设计模式这一编程领域的核心概念。设计模式是软件工程中的一种最佳实践,它在解决常见问题时提供了一种标准的解决方案,使得代码更易于...

    23种设计模式学习笔记及源码

    这个压缩包文件包含了23种设计模式的学习笔记和源码,旨在帮助开发者深入理解并熟练运用这些模式。以下是对每种设计模式的详细解释,以及它们在Java编程中的应用。 1. **单例模式**:确保一个类只有一个实例,并...

    C#设计模式学习笔记

    C#设计模式学习笔记是一份详尽的资源,适合任何希望深入了解如何在C#编程中应用设计模式的开发者。这份笔记涵盖了多种设计模式,旨在提升代码的可读性、可维护性和可扩展性,这些都是软件开发中至关重要的要素。 ...

    《C++20设计模式》学习笔记-第12章代理模式-配套代码

    《C++20设计模式》学习笔记-第12章代理模式-配套代码

    16-Java代理模式的学习笔记1

    Java代理模式是一种设计模式,它允许我们为一个对象创建一个代理,这个代理对象可以在调用原始对象的方法之前或之后执行额外的操作。代理模式在软件工程中有多种应用,例如:增加安全控制、性能监控、事务管理等。...

    设计模式学习笔记及其模式特点总结

    代理模式分为静态代理和动态代理,其中动态代理在Java中可以通过InvocationHandler接口和Proxy类实现。 2. **单例模式**: 单例模式确保一个类只有一个实例,并提供全局访问点。这种模式常用于控制资源的共享,如...

    设计模式学习笔记

    7. 代理模式(Proxy):为其他对象提供一种代理以控制对这个对象的访问。 行为型模式涉及对象之间的责任分配,包括: 1. 责任链模式(Chain of Responsibility):将请求沿着处理者链传递,直到有对象处理为止。 2....

    设计模式之美—学习笔记

    在这个“设计模式之美”的学习笔记中,我们将探讨一些主要的设计模式,以及它们在实际开发中的应用。 首先,我们从创建型模式开始。这类模式主要用于对象的创建,如单例模式(Singleton)、工厂模式(Factory ...

    《设计模式》学习笔记

    ### 设计模式学习笔记 #### 引言 设计模式(Design Patterns)是在软件设计领域内广泛应用的一种实践指南,它提供了一系列解决常见问题的方案。设计模式可以被理解为面向对象软件设计的经验总结,是对特定面向对象...

    Head First 设计模式学习笔记(十四)模式的组合使用

    在《Head First 设计模式学习笔记(十四)模式的组合使用》中,作者探讨了如何在实际编程中灵活地组合多种设计模式以解决复杂问题。这篇文章可能是基于《Head First 设计模式》这本书的一个章节,该书是设计模式领域...

    java设计模式学习笔记,以及各种模式的代码实现

    9. **代理模式**:为其他对象提供一种代理以控制对这个对象的访问。它可以用于远程代理、虚拟代理、缓存等。 10. **桥接模式**:将抽象部分与它的实现部分分离,使它们都可以独立地变化。这种设计模式属于结构型...

    #资源大人分享计划# 设计模式学习笔记.rar

    在这个“#资源大人分享计划# 设计模式学习笔记.rar”压缩包中,我们可以期待找到一系列关于设计模式的详细学习资料,包括定义、类图、代码示例以及各种使用场景,这些都将帮助我们深入理解并掌握设计模式。...

    Java 版设计模式学习笔记-java-design-patterns.zip

    这个“Java版设计模式学习笔记”涵盖了多种设计模式,旨在帮助开发者更好地理解和应用这些模式。让我们深入探讨一下其中可能包含的关键知识点。 一、单例模式 单例模式确保一个类只有一个实例,并提供一个全局访问...

    GoF 23种设计模式学习笔记

    "GoF 23种设计模式学习笔记" 是一个深入探讨这23个经典设计模式的资源,这些模式最初由Erich Gamma、Richard Helm、Ralph Johnson和John Vlissides四位作者在1994年的著作《设计模式:可复用面向对象软件的基础》中...

    Spring学习笔记(13)----动态代理模式分析演示

    在本篇Spring学习笔记中,我们将深入探讨动态代理模式,并结合Spring框架的实现进行分析演示。动态代理模式是Java编程中一种重要的设计模式,它允许我们在不修改原对象代码的情况下,为对象添加额外的功能或行为。...

Global site tag (gtag.js) - Google Analytics