如果在代理模式中,我们有接口而不是实际具体的代理类中的主题等同于装饰器模式 [英] If In Proxy Pattern we have interface instead of actual concrete Subject in Proxy class is it equivalent to Decorator Pattern

查看:198
本文介绍了如果在代理模式中,我们有接口而不是实际具体的代理类中的主题等同于装饰器模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代理模式在执行一些额外处理之后,将请求委托给Real主题,例如,如果请求需要被处理,则可能需要进行一些凭证检查。

Proxy pattern delegates the request to the Real subject after doing some additional processing like applying checks if request needs to be processed or not based on may be some credential checks.

它有下面的类图

代理课程直接参考具体主题。

Proxy class has a direct reference to the concrete Subject.

装饰图案丰富了组件的行为[像代理它也做了一些额外的处理,并将操作委托给真实的组件]。这种模式的类图与Proxy模式类似,只不过是引用了组件的接口。

Decorator Pattern enriches the behavior of the component [like proxy it also does some additional processing and delegates the operation to the real component]. The class diagram of this pattern is similar to Proxy pattern with only difference being it has the reference to the interface of the component.

在Proxy类中具有实际的主题使得单元测试变得困难,因为类只应该依赖于接口而不是实现。我的问题是,如果代理模式也引用由Real主题公开的界面,那么它将等同于Decorator模式。在这种情况下,代理模式的类图也将变为以下

Having concrete real subject in Proxy class makes it difficult for unit testing as Classes should only be dependent upon interfaces and not the implementations. My Question is if Proxy pattern also has the reference to the interface exposed by the Real subject then will it be equivalent to the Decorator pattern. In such a case the class diagram of the proxy pattern will also become as below

推荐答案

这一切都是为了意图。在功能上,它们将是等效的,但是装饰器的要点是动态地向对象添加特征,而代理只是控制对目标对象的访问,而不添加任何其他功能。

It's all about the intent. Functionally, they will be equivalent, but the point of decorator is to add features to an object dynamically, while proxy just controls the access to the target object without adding any additional features to it.

因此,代理的客户端期望与使用真实对象一样的结果,而装饰器的客户端将其提交给装饰器,以便在将呼叫委派给目标之前和/或之后执行任何附加逻辑

So the client of the proxy expects the same outcome as if it worked with the real object, while the client of the decorator leaves it to the decorator to execute any additional logic before and/or after delegating the call to the target.

所以,在概念上看来,在你的例子中,你仍然在处理代理。

So, conceptually it seems that in your example you're still dealing with a proxy.

这篇关于如果在代理模式中,我们有接口而不是实际具体的代理类中的主题等同于装饰器模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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