代理模式和装饰模式的区别 [英] Differences between Proxy and Decorator Pattern

查看:16
本文介绍了代理模式和装饰模式的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能很好地解释一下代理装饰器之间的区别是什么?

Can you give any good explanation what is the difference between Proxy and Decorator?

我看到的主要区别是,当我们假设 Proxy 使用 compositionDecorator 使用 aggregation 时似乎很明显,通过使用多个(一个或多个)装饰器,您可以修改/添加功能到预先存在的实例(装饰),而 代理 具有自己的内部实例代理类和委托给它添加一些额外的功能(代理行为).

The main difference I see is that when we assume that Proxy uses composition and Decorator uses aggregation then it seems to be clear that by using multiple (one or more) Decorators you can modify/ add functionalities to pre-existing instance (decorate), whereas Proxy has own inner instance of proxied class and delegates to it adding some additional features (proxy behaviour).

问题是 - 使用聚合创建的 Proxy 仍然是 Proxy 还是 Decorator?是否允许(根据 GoF 模式中的定义)使用聚合创建 Proxy?

The question is - Does Proxy created with aggregation is still Proxy or rather Decorator? Is it allowed (by definition in GoF patterns) to create Proxy with aggregation?

推荐答案

这是 GoF 的直接引述(第 216 页).

Here is the direct quote from the GoF (page 216).

虽然装饰器可以有与代理类似的实现,但装饰器有不同的用途.装饰器为对象添加一个或多个职责,而代理控制对对象的访问.

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.

代理的实现程度不同,就像装饰器一样.一个保护代理可能完全像装饰器一样实现.在另一一方面,远程代理将不包含对其真实主题的直接引用,而只包含间接引用,例如主机 ID 和主机上的本地地址".虚拟代理将从间接引用(例如文件名)开始,但最终会获取并使用直接引用.

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.

流行的答案表明代理知道其委托的具体类型.从这句话中我们可以看出这并不总是正确的.

Popular answers indicate that a Proxy knows the concrete type of its delegate. From this quote we can see that is not always true.

根据 GoF,Proxy 和 Decorator 的区别在于,Proxy 限制客户端.装饰器没有.代理可以通过控制对功能的访问来限制客户端做什么;或者它可以通过执行客户端不可见和不知道的操作来限制客户端知道.装饰器则相反:它以客户端可见的方式增强其委托的功能.

The difference between Proxy and Decorator according to the GoF is that Proxy restricts the client. Decorator does not. Proxy may restrict what a client does by controlling access to functionality; or it may restrict what a client knows by performing actions that are invisible and unknown to the client. Decorator does the opposite: it enhances what its delegate does in a way that is visible to clients.

我们可以说 Proxy 是一个黑盒,而 Decorator 是一个白盒.

We might say that Proxy is a black box while Decorator is a white box.

在对比代理和装饰器时,包装器和委托之间的组合关系是错误的关系,因为组合是这两种模式的共同特征.包装器和客户端之间的关系是区分这两种模式的原因.

The composition relationship between wrapper and delegate is the wrong relationship to focus on when contrasting Proxy with Decorator, because composition is the feature these two patterns have in common. The relationship between wrapper and client is what differentiates these two patterns.

  • 装饰者通知并授权其客户.
  • 代理限制并剥夺其客户的权力.

这篇关于代理模式和装饰模式的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆