代理模式和适配器模式的区别?
看来Proxy模式和Adapter模式有相似之处?
任何人都可以解释一下有什么区别吗?为什么我们需要两者?在 .net 示例中,在哪些问题中我们应该仅使用代理而不是另一个?
谢谢
It seems that there are similarities between Proxy and Adapter pattern?
Can any one please explain what are the differences? Why we require both of them? In which problems we should use only proxy and NOT another by an .net example?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代理公开与其隐藏的对象完全相同的行为。代理通常用于联系远程对象,而无需知道如何联系它。一个例子是 WCF 服务,您可以将访问该服务封装在代理中,该代理公开与 wcf 服务完全相同的接口,但隐藏实现细节,例如使用通道工厂和处理故障异常等...就像您的客户端在说话一样到本地的WCF服务。
适配器还隐藏底层对象,但它将您与之交换的数据转换为底层对象使用的正确格式和内容。正如 Goblin 所说,一个例子确实是遗留系统。您将与遗留系统通信的复杂性(也许它使用了繁琐的或 CRUDy API,并且您希望将其隐藏在粗粒度操作后面)封装到适配器中,以向您的客户端提供与遗留系统通信的简单方法。
至少我是这么理解的。
编辑:顺便说一句,我个人认为您不必将设计模式名称视为万能的。根据您想要实现的目标选择正确的模式,并随心所欲地命名它。
A proxy exposes the exact same behavior as the object it hides. A proxy is typically used to contact a remote object without having to know how to contact it. An example is a WCF service, you can encapsulate accessing the service in a proxy that exposes the exact same interface as the wcf service, but hides the implementation details away like using a channelfactory and handling faultexceptions etc... It's like you client is talking to the WCF service locally.
An adapter also hides an underlying object, but it transforms the data you exchange with it to the right format and content used by the underlying object. An example is indeed a legacy system, like Goblin says. You encapsulate the complexity of talking to the legacy system (maybe it uses a chatty or CRUDy API and you want to hide it behind a coarse-grained operation) into an adapter to prvide a simple way of talking to the legacy system to your clients.
That's how I understand it at least.
EDIT: by the way, I personally feel that you don't have to see design pattern names as the end-all-do-all. Choose the right pattern based on what you want to achieve and call it whatever you want.
代理通常用于以下场景:
适配器还扮演着另一个角色——它们在两个没有关系的类之间架起桥梁。适配器可以充当这两个对象。这主要用于必须与无法更改 API 的遗留系统(或第三方框架)集成时。
希望这有帮助!
Proxies are typically used for the following scenarios:
Adaptors play another role - they bridge the gap between two classes that have no relation. The Adaptor can act as both objects. This is used primarily when one has to integrate with legacy systems (or 3rd party frameworks for that matter) where it is not possible to alter the API.
Hope this helps!