问题描述
您可以给出任何好的解释
我看到的主要区别是,当我们假设 proxy 使用 coptosion 和 decorator 使用 cotegration 似乎很明显,通过使用多个(一个或多个) decorator 您可以修改/添加功能到预先存在的实例(装饰),而 proxy 有自己的内在实例代理类和代表的代表添加了一些其他功能(代理行为).
问题是 - 代理用聚合创建的代理还是 Decorator ?是否允许(按照GOF模式定义)可以创建代理?
推荐答案
这是GOF的直接报价(第216页).
尽管装饰师可以具有与代理相似的实现,但装饰器的目的不同.装饰器向对象添加一个或多个责任,而代理控制对象的访问.
代理在其实施程度上像装饰器一样不同.一个 保护代理可能完全像装饰器一样实现.在另一 手,远程代理不会直接引用其真实主题,而仅包含 间接参考,例如"主机上的主机ID和本地地址".虚拟代理 将从间接参考(例如文件名)开始,但最终将 获取并使用直接参考.
流行的答案表明,代理人知道其代表的具体类型.从这个报价中,我们可以看到这并不总是正确的.
根据GOF,代理和装饰器之间的区别在于代理限制了客户端.装饰者没有.代理可以通过控制对功能的访问来限制客户端所做的事情;或者,它可能会通过执行不可见且未知的操作来限制客户端所知道的. Decorator的做法相反:它以客户可见的方式增强了代表的工作.
我们可能会说代理是一个黑匣子,而装饰师是白盒子.
包装纸和代表之间的组成关系是在将代理与装饰器进行对比时的错误关系,因为组成是这两种模式的共同特征.包装器和客户之间的关系是区分这两种模式的方法.
- Decorator通知并授权其客户.
- 代理限制并剥夺了其客户的权力.
其他推荐答案
真正的差异不是所有权(组成与聚合),而是类型信息.
a 装饰器 is 始终通过其代表. 代理 可能自己创建它,或者他可能注入了它.
但代理 总是知道代表的(更多)特定类型.换句话说,代理及其代表将具有相同的基本类型,但是代理指向某种派生类型. 装饰指向其自己的基本类型.因此,区别在于有关代表类型的编译时间信息.
在动态语言中,如果将代表注入并且恰好具有相同的界面,则没有区别.
您问题的答案是"是".
其他推荐答案
装饰器模式集中于动态添加功能,而代理模式集中于控制对对象的访问.
编辑: -
A 代理与真实主题之间的关系通常是在编译时间设置, proxy 以某种方式实例化,而 decorator 是分配的在运行时对主题,只知道主题的界面.
问题描述
Can you give any good explanation what is the difference between Proxy and Decorator?
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).
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?
推荐答案
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.
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.
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.
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.
- Decorator informs and empowers its client.
- Proxy restricts and disempowers its client.
其他推荐答案
The real difference is not ownership (composition versus aggregation), but rather type-information.
A Decorator is always passed its delegatee. A Proxy might create it himself, or he might have it injected.
But a Proxy always knows the (more) specific type of the delegatee. In other words, the Proxy and its delegatee will have the same base type, but the Proxy points to some derived type. A Decorator points to its own base type. Thus, the difference is in compile-time information about the type of the delegatee.
In a dynamic language, if the delegatee is injected and happens to have the same interface, then there is no difference.
The answer to your question is "Yes".
其他推荐答案
Decorator Pattern focuses on dynamically adding functions to an object, while Proxy Pattern focuses on controlling access to an object.
EDIT:-
Relationship between a Proxy and the real subject is typically set at compile time, Proxy instantiates it in some way, whereas Decorator is assigned to the subject at runtime, knowing only subject's interface.