代理和装饰器模式之间的差异 [英] Differences between Proxy and Decorator Pattern

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

问题描述

你可以给出任何很好的解释,代理和装饰器之间的区别是什么?

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

主要区别我看到,当我们假设代理使用组合 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).

问题是 - 使用聚合创建的代理仍然是代理或者装饰器?是否允许(根据GoF模式的定义)使用聚合创建代理

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?

推荐答案

装饰器模式着重于将动态添加到对象的功能,而代理模式侧重于控制对象的访问。

Decorator Pattern focuses on dynamically adding functions to an object, while Proxy Pattern focuses on controlling access to an object.

编辑: -

代理与真实主题之间的关系通常在编译时设置,代理以某种方式实例化,而在运行时将装饰器分配给主题,只知道主体的界面。

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.

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

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