最佳使用设计模式:适配器或外观

发布于 2024-09-09 07:29:27 字数 166 浏览 21 评论 0原文

我无法决定哪种模式最适合以下问题。

我有一个客户端系统,它将与一个单独的子系统交互。该子系统非常复杂,因此我需要两者之间的接口来简化客户端系统。这听起来非常适合外观模式,但我认为适配器模式也适合我的问题。

如果中间的接口通过简单的 API 调用来调用子系统上的各个任务,会有什么区别吗?

I can't decide on which pattern is best suited to the following problem.

I have a client system who will be interacting with a separate subsystem. The subsystem is quite complicated and so I need an interface between the two to simplify the client system. This sounds like a perfect fit for Facade pattern but I think Adapter pattern too fits for my problem.

Does it make any difference if the interface in the middle calls individual tasks on the subsystem through simple API calls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

眼泪也成诗 2024-09-16 07:29:27

从你的描述来看,它更符合外观的公认定义,但我想说它更多的是语义辩论。一般来说,外观更多地降低了与整个子系统接口的复杂性,而适配器更适合调整现有接口或调用您的特定需求(例如,基本功能已存在,但返回类型并不完全是您想要的, ETC)。

From your description its more in-line with the accepted definition of the Facade, but I'd say its more of a semantic debate than anything else. Facade in general is more reducing the complexity of interfacing with an entire subsystem, whereas adapter is more geared towards tweaking an existing interface or call to your specific needs (eg the basic functionality is there but the return types aren't quite what you want, etc).

我不是你的备胎 2024-09-16 07:29:27

当您想要将现有类的接口调整为客户端期望使用的另一个接口时,可以使用适配器模式。它通常只涉及从一个接口的方法到另一个接口的相应方法的委托或转换。

当您想要通过公开客户端可以使用的一组更简单的 API 来简化复杂的系统时,可以使用外观。它涉及将复杂的 API 调用模式转换为单个 API 调用。

你的情况听起来更像是你需要一个外观而不是一个适配器。仅实现适配器模式不会给您带来 API 简化的好处。最后,你怎么称呼它并不重要。而且这些模式并不是排他性的。您可以以给您带来最大利益的方式混合两者。

Adapter pattern is used when you want to adapt an existing class's interface to another interface that a client expects to work with. It usually just involves delegation or translation from a method of one interface to the corresponding method of the other.

Facade is used when you want to simplify a complex system by exposing a simpler set of APIs that a client can work with. It involves translating a complex pattern of API calls into a single API call.

Your case sounds more like you need a facade than an adapter. Implementing just the adapter pattern will not give you the benefit of API simplification. In the end, it doesn't matter what you call it. And these patterns are not exclusive. You could mix both in a way that gives you the most benefit.

眼前雾蒙蒙 2024-09-16 07:29:27

这显然似乎是一个外观模式案例,您的目标是简化而不是实际的适应。

立面定义:

为集合提供统一的接口
子系统中的接口。正面
定义了一个更高级别的接口
使子系统更易于使用。

适配器定义:

将类的接口转换为
客户期望的另一个界面。
适配器让类一起工作
否则不能因为
接口不兼容。

定义摘自:http://dofactory.com/Patterns/Patterns.aspx

This clearly seems to be a Facade pattern case your goal is simplification rather than an actual adaptation.

Façade Definition:

Provide a unified interface to a set
of interfaces in a subsystem. Façade
defines a higher-level interface that
makes the subsystem easier to use.

Adapter Definition:

Convert the interface of a class into
another interface clients expect.
Adapter lets classes work together
that couldn't otherwise because of
incompatible interfaces.

Definitions lifted from: http://dofactory.com/Patterns/Patterns.aspx

安静被遗忘 2024-09-16 07:29:27

Facade 和 Adapter 之间的区别主要在于意图。

如果您想做的是简化界面,那么您正在寻找外观。如果你想调整接口以便它可以用作其他东西,那么它就是一个适配器。

但说实话,你怎么称呼它,问题是什么?我的经验法则是,如果您正在实现现有接口,您可能正在使用适配器接口。如果您正在创建一个新的简化界面,那么它就是一个外观。

The difference between Facade and Adapter is mostly the intent.

If what you want to do is simplify the interface then you are looking at a Facade. If you want to Adapt the interface so that it can be used as something else then its an Adapter.

But really, what is the problem how you call it? My rule of thumb is if you are implementing an existing interface you probably are using the Adapter interface. If you are creating a new simplified interface its a Facade.

情场扛把子 2024-09-16 07:29:27

外观模式(为更大的代码体提供简化接口的对象< /em>) 适合您的用例。

使用外观的清单:(来自链接的维基百科文章)

  1. 访问复杂的系统需要一个简单的界面。
  2. 子系统的抽象和实现是紧密耦合的。
  3. 分层软件的每个级别都需要一个入口点。
  4. 系统非常复杂或难以理解。

有关 Facade 的更多详细信息,请参阅相关 SE 问题。

什么是外观设计模式?

尽管外观< /em> 和 Adapter 是结构模式,意图不同( samitgaur 答案很好地解释了意图部分)。

由于您没有将一个接口转换为另一接口,因此适配器无法满足您的目的。

相关SE问题:

有什么区别外观和适配器模式之间?

Facade pattern (An object that provides a simplified interface to a larger body of code) fits for your use case.

Checklist to use Facade: ( From linked Wikipedia article)

  1. A simple interface is required to access a complex system.
  2. The abstractions and implementations of a subsystem are tightly coupled.
  3. Need an entry point to each level of layered software.
  4. System is very complex or difficult to understand.

Related SE question for more details on Facade.

What is Facade Design Pattern?

Even though both Facade and Adapter are structural patterns, the intent is different( samitgaur answer explained the intent part nicely).

Since you are not converting one interface to other interface, Adapter does not serve your purpose.

Related SE question:

What is the difference between the Facade and Adapter Pattern?

复古式 2024-09-16 07:29:27

外观处理的是接口,而不是实现。其目的是将内部复杂性隐藏在看似简单的单一界面后面。

Facade deals with interface, not implementation. Its purpose is to hide internal complexity behind a single interface that appears simple on the outside.

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