无法理解代理的缺点 [英] Not able to understand the Proxy cons

查看:67
本文介绍了无法理解代理的缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这篇文章中阅读了有关代理模式的内容,并理解了示例,但是这条评论让我感到困惑.评论指出:

I was reading about proxy pattern in this article and understood the example, but this commentconfused me. The comment states:

代理有两个大问题,尤其是在企业环境中.

There are 2 big problems with proxies, especially in enterprise environments.

1) 您不能进行自我调用.一个很好的例子是执行事务管理或安全性的代理.因此,您需要确保不是进行自我调用,而是将调用转发给代理.这使得简单的类变得复杂.

1) You can't do self calls. A good example is a proxy that does transaction management or security. So you need to make sure that instead of doing a self call, you forward the call to the proxy. This makes simple classes complex.

2) 身份有问题;一个很好的例子是一个休眠代理,它使得不可能进行引用比较,即使你得到保证在一个会话中没有同一实体的多个对象实例.

2) There are issues with identity; a good examlpe is a hibernate proxy which makes it impossible to do a reference comparison, even though you get the guarantee that there are not multiple object instances of the same entity in a session.

我的问题:

  1. 自呼是什么意思?
  2. 当两者都引用代理对象时,为什么我们不能进行引用比较?

推荐答案

  1. 自调用"是指在同一对象上调用方法,使用otherMethod() 而不是var.otherMethod().由于对同一对象的调用直接通过 this 而不是代理,因此不会发生代理应用的任何特殊处理.

  1. "Self call" refers to calling a method on the same object, using otherMethod() instead of var.otherMethod(). Since the call on the same object goes straight through this and not the proxy, any special treatment that the proxy would apply doesn't happen.

运行时环境可能会摆弄代理对象,并且您总是冒着意外尝试将代理本身与隐藏在其后面的业务对象进行比较的风险,例如在对象传递 this 在某处引用,有人试图比较它从运行时查找它(并获取代理).

The runtime environment may fiddle around with the proxy object, and you always run the risk of accidentally trying to compare the proxy itself with the business object hiding behind it, as in the case where the object passes a this reference out somewhere and somebody trying to compare it looks it back up from the runtime (and gets the proxy).

这篇关于无法理解代理的缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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