代理设计模式:缺点 [英] Proxy Design Pattern : Disadvantages

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

问题描述

我正在阅读有关代理模式的文章之一.

I was going through one of the Articles on Proxy pattern.

在解释后阅读评论

在本文中,代理模式提到了一些缺点,但我无法理解:

In this article there are few downsides mentioned for Proxy Patterns, but I am not able to understand:

1)不利的一面是,扩展程序没有意识到(黑匣子"问题),这可能是魔术".请解释魔术.

1) The downside here is 'magic' could be happening that an extender is unaware of (a 'black-box' problem). Please explain the magic.

2)代理可以掩盖来自客户端的易失性资源的生命周期和状态.客户端可能会呼叫代理而不意识到资源当前不可用...在这种情况下,代理必须阻塞直到资源再次可用,否则它必须产生某种错误.用Java术语来说,它必须是未经检查的异常,因为Proxy必须符合原始对象的接口.另外,客户端可能不知道它现在正在调用的资源与第二次调用的资源不同.如果资源上存在任何状态,则客户端可能会感到状态似乎已被遗忘.

2) A proxy can mask the life-cycle and state of a volatile resource from its client. A client may call the proxy not realizing that the resource is currently unavailable... in this case the proxy has to either block until a resource is available again, or it must produce some kind of error. In Java terms it would have to be an unchecked exception, since the Proxy must comply with the interface of the original object. Also the client may not be aware that the resource it is calling now is not the same resource it called a second ago; if there is any state on the resource then the client may be confused that the state appears to have been forgotten.

请解释.

3)如果在本地进程中使用代理来表示远程资源,则可以掩盖涉及远程通信的事实.众所周知,远程调用与本地调用完全不同,我们的程序不应将其视为相同.如果代理以某种方式声明它是远程资源而不是本地资源的代理,则更好.这样,客户端将只能选择本地资源,或在使用远程资源时修改其行为.

3) if a proxy is used to represent a remote resource in the local process, this can disguise the fact that remote communication is involved. As we know, remote invocation is completely different from local invocation, and our programs should not treat it as if it were the same. It is better if the proxy declares somehow that it is a proxy for a remote resource, rather than a local resource. Then clients would have be able to choose only local resources, or to modify their behavior when using a remote resource.

您能否帮助我理解上述与代理的缺点有关的三点?

推荐答案

这提出了3个不同的问题.我会回答第三个问题.您最好将问题编辑为一个问题,然后在一个单独的问题中询问另外两个问题.

That makes 3 different questions. I'll answer the third one. You'd better edit your question to a single one, and ask each of the two others in a separate question.

在处理与远程服务器的通信时,通常使用代理模式(例如,通过RMI).您从某个工厂获得了对对象的引用,实际上您得到的是一个存根(代理),对于您调用的每个方法,该存根都会对方法的参数进行序列化,然后将其发送到服务器,等待响应,并返回结果.代理使这几乎透明,但是却不知道所有这些事情都是在后台发生的,这会使您编写代码的效率很低.

When dealing with communication with a remote server, the proxy pattern is often used (by RMI, for example). You get a reference to an object from some factory, and what you get is in fact a stub (a proxy) which, for every method you call, serilaizes the arguments of the method, sends them to a server, waits for the response, and returns the result. The proxy makes that almost transparent, but not being aware that all this happens behind the scenes can make you code things very inefficiently.

以以下示例为例:

if (account.getBalance() > 0 && account.getBalance() < MAX) {
    transferAmount(account.getBalance() / 2);
}

现在假设 account 是远程对象的代理.每次调用 getBalance()时,都会进行一次远程网络调用,这有可能导致异常,甚至每次都返回一个不同的值,从而使此简单的代码段效率极低.

Now imagine that account is a proxy to a remote object. Each time getBalance() is called, a remote network call is made, which can potentially lead to an exception, or even return a different value each time, making this simple code snippet extremely inefficient.

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

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