原版设计模式之代理模式

Intent (定义)

Provide a surrogate or placeholder for another object to control access to it.(为另一个对象提供代理或占位符,以控制对它的访问。

Also Known As (别名)

Surrogate

Motivation (详细说明)

图像编辑器为例,图像编辑器有很多不同的图像,在需要时使用。
在打开图像编辑器时,如果一次性创建很多图像,则响应过慢。
The solution is to use another object, an image proxy, that acts as a stand-in for the real image. The proxy acts just like the image and takes care of instantiating it when it’s required.(解决方案是使用图像代理,作为真实图像的替代品。代理就像图像一样工作,并在需要时负责实例化它。
在这里插入图片描述
The image proxy creates the real image only when the document editor asks it to display itself by invoking its Draw operation. The proxy forwards subsequent requests directly to the image. It must therefore keep a reference to the image after creating it.(当文档编辑器调用Draw方法来显示图像时,图像代理才会创建真实图像。代理将后续请求直接转发到图像。因此,图像代理必须在创建该图像后保留对该图像的引用。
Let’s assume that images are stored in separate files. In this case we can use the file name as the reference to the real object. The proxy also stores its extent, that is, its width and height. The extent lets the proxy respond to requests for its size from the formatter without actually instantiating the image.(假设图像存储在单独的文件中。在这种情况下,我们可以使用文件名作为对真实对象的引用。代理还存储它的宽度和高度。宽高能让代理响应格式化程序中关于其大小的请求,而不用实例化图像。
在这里插入图片描述
The document editor accesses embedded images through the interface defined by the abstract Graphic class. (文档编辑器通过抽象Graphic类定义的接口访问嵌入的图像
ImageProxy is a class for images that are created on demand. (ImageProxy是一个按需创建的图像类。
ImageProxy maintains the file name as a reference to the image on disk. The file name is passed as an argument to the ImageProxy constructor.(ImageProxy将文件名作为对磁盘上图像的引用。将文件名作为参数传递给ImageProxy构造函数。

Applicability (适用点)

  • 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.”(远程代理为不同地址空间中的对象提供了一个本地代表。NEXTSTEP 为此目的使用了类NXProxy。Coplien 称这种代理方式为“Ambassador”。
  • A virtual proxy creates expensive objects on demand. The ImageProxy described in the Motivation is an example of such a proxy.(虚拟代理可以按需创建代价高的对象。Motivation 中描述的图像代理就是这种代理的一个例子。
  • 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.(例如,在kernel代理中,登录系统的用户不同,访问权限不同
  • A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed. (智能引用是在访问对象时可以做其他操作。
    Typical uses include(典型的例子如下:
    • 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]).(计算对真实对象的引用数,当没有更多的引用时可以自动释放此对象(也称为智能指针[Ede92])。
    • loading a persistent object into memory when it’s first referenced.(第一次引用持久化对象时,加载到内存中
    • checking that the real object is locked before it’s accessed to ensure that no other object can change it.(在访问真实对象之前,检查该对象是否被锁定【然后加锁】,以确保没有其他对象可以更改它。

Structure (结构)

在这里插入图片描述
运行时访问过程如下:
在这里插入图片描述

Participants (参与者)

  • Proxy (ImageProxy)(代理
    • maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.(维护允许代理访问真实对象的引用。如果RealSubject 和Subject 接口相同,Proxy 可以指向Subject 【多态】。
    • provides an interface identical to Subject’s so that a proxy can by substituted for the real subject.(提供与Subject相同的方法,以便Proxy可以替代RealSubject 。
    • controls access to the real subject and may be responsible for
      creating and deleting it.(控制对RealSubject 的访问,并负责创建和删除它。
    • other responsibilities depend on the kind of proxy:(其他取决于代理类型
      • remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.(远程代理负责对请求及其参数进行编码,并将所编码后的请求发送到不同地址的RealSubject 。
      • virtual proxies may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real image’s extent.(虚拟代理可以缓存RealSubject 的其他信息,以便可以推迟访问RealSubject 。例如,Motivation 中ImageProxy 缓存了真实图像的范围。【上述案例】
      • protection proxies check that the caller has the access permissions required to perform a request.(保护代理会检查调用者是否具有执行请求的访问权限。
  • Subject (Graphic)
    • defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.(定义RealSubject 和Proxy 的公共方法,以便可以在使用RealSubject 的任何地方使用Proxy 。【Proxy 替代RealSubject 】
  • RealSubject (Image)(被代理对象
    • defines the real object that the proxy represents(定义代理所代表的真实对象

Collaborations (约定)

Proxy forwards requests to RealSubject when appropriate, depending on the kind of proxy.(对于不同的代理类型,代理会在适当的时候将请求转发给RealSubject 。

Related Patterns (相关模式)

Adapter (157): 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 (196): 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.(代理像装饰器一样实现的程度不同。保护代理可以完全像装饰器一样实现。
另一方面,远程代理将不会包含对其真实对象的直接引用,而是只有间接的引用,如“主机ID和主机上的本地地址[【mac】”。
虚拟代理将以一个间接引用开始,如文件名,但最终将获取并使用一个直接引用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值