Proxy Design Pattern : Disadvantages

左心房为你撑大大i 提交于 2020-01-17 04:35:09

问题


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

Read the Comments After the Explanation

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

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

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.

Please explain.

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.

Would you please help me in understanding three point above related to the downsides of Proxy?


回答1:


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.

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.

Take this example for example:

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

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.



来源:https://stackoverflow.com/questions/18302183/proxy-design-pattern-disadvantages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!