代理模式和适配器模式的区别?

发布于 2024-09-24 06:45:32 字数 112 浏览 15 评论 0原文

看来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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔日梦未散 2024-10-01 06:45:32

代理公开与其隐藏的对象完全相同的行为。代理通常用于联系远程对象,而无需知道如何联系它。一个例子是 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.

甜心小果奶 2024-10-01 06:45:32

代理通常用于以下场景:

  • 创建底层“真实”对象的成本很高。然后,您可以在创建代理时将其作为占位符(下载大图像时的进度条图标是一个典型的示例)。延迟加载是另一个典型的例子。我们的想法是,我们不知道用户是否会单击“详细信息”窗格 - 因此我们将推迟加载,直到他实际单击它或系统空闲。
  • 您想要控制对代理内“真实”对象的部分或全部成员的访问 (SecurityProxy)。

适配器还扮演着另一个角色——它们在两个没有关系的类之间架起桥梁。适配器可以充当这两个对象。这主要用于必须与无法更改 API 的遗留系统(或第三方框架)集成时。

希望这有帮助!

Proxies are typically used for the following scenarios:

  • The underlying 'real' object is expensive to create. You then have the proxy be a placeholder while it is created (a progressbar icon while downloading a huge image is a typical example). Lazy-loading is another typical example. The idea being that we don't know if the User will ever click the 'Details' pane - so we'll defer loading till he actually clicks it or the system is idle.
  • You want to control access (SecurityProxy) to some or all members of the 'real' object within the proxy.

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文