未使用的接口参数

发布于 2024-12-08 08:51:52 字数 367 浏览 0 评论 0原文

我有一个由三十个具体类实现的接口。具体实现者分为两组,每组都继承自一个公共抽象类。抽象类为具体实现者定义构造函数,包括为两侧的每个“侧”传入数据库连接对象(除了其他差异之外,它们具有不同的数据库)。

当前的所有接口方法均具有具体类“完成工作”所需的多个参数,但并非所有实现者都使用所有参数。

今天早上,当我向接口添加一个新方法时,我意识到只有一个具体实现者需要数据库连接,而其余的实现者则不需要它。所以,这让我想知道,我应该将它作为参数传递吗?需要“完成工作”,但仅针对其中一个具体类,并且该类已经具有数据库连接。如果我将数据库连接作为接口参数传入,那么其他 29 个类将不会使用它。

对于可接受的接口参数,应该画一条什么样的线?任何有关该主题的阅读/内容我也会感激不尽。

I have an interface that is implemented by thirty concrete classes. The concrete implementers are subdivided into two groups, with each group inheriting from a common abstract class. The abstract classes define the constructors for the concrete implementers, including passing in a database connection object for each "side" of the two sides (they have different databases, among other differences).

All the current interface methods each have several parameters needed for the concrete classes to "get the job done", but not all are used in every implementer.

When I went to add a new method to the interface this morning, I realized that the database connection is going to be needed for only one of the concrete implementers, but the rest will not need it. So, that gets me wondering, should I pass it in as a parameter? It is needed to "get the job done", but for only one of the concrete classes, and that class has the database connection already. If I passed the database connection in as an interface parameter, then the other 29 classes will not use it.

What is a good line to draw for what is an acceptable interface parameter? Any reading/content on the subject I will thankfully devour as well.

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

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

发布评论

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

评论(4

提笔书几行 2024-12-15 08:51:52

目前所有的接口方法都需要几个参数
用于具体类“完成工作”,但并非所有类都被使用
在每个实施者中。

在我看来,这个界面正在慢慢变成一个“上帝界面” 。通过问自己几个问题来检查情况是否如此:

  • 该接口是否代表模型中的单个行为概念,或者它是否已成为多个概念的方法签名的便捷垃圾场?是否可以将其称为 Serialized 之类的名称,或者更准确地称为 SerializedAndSomethingElse
  • 您能否将接口分成几个更具内聚性的接口,并让 30 个不同的对象只实现它们需要的对象?

今天早上当我去向界面添加一个新方法时,我
意识到数据库连接仅需要
具体实施者之一,但其他人则不需要它。所以,
这让我想知道,我应该将它作为参数传递吗?

不。事实上,如果数据库连接仅由其中一个实现者需要,那么听起来它根本不属于接口。接口应该代表抽象 API,听起来好像数据库连接是该 API 实现的一部分。

All the current interface methods each have several parameters needed
for the concrete classes to "get the job done", but not all are used
in every implementer.

That sounds to me a lot like the interface is slowly turning into a bit of a "god interface". Check whether this is the case by asking yourself a couple of questions:

  • Does the interface represent a single behavioural concept in your model, or has it become a bit of a convenient dumping ground for method signatures from several concepts? Could it be called something like e.g. Serializable, or would it more accurately be called SerializableAndSomethingElse.
  • Could you carve the interface up into several more cohesive interfaces, and have the 30 different objects implement just the ones they need?

When I went to add a new method to the interface this morning, I
realized that the database connection is going to be needed for only
one of the concrete implementers, but the rest will not need it. So,
that gets me wondering, should I pass it in as a parameter?

No. In fact, if the database connection is only needed by one of the implementers then it doesn't sound like it belongs in the interface at all. The interface should represent the abstract API, where as it sounds as though the database connection is a part of the implementation of that API.

亣腦蒛氧 2024-12-15 08:51:52

如果它不是抽象的一部分——那么它就不应该出现在接口中。如果它只被 30 个实现类中的 1 个使用,那么它绝对不是抽象的一部分。

我在谷歌上快速搜索了“api design”,第一个结果是:

演示文稿的幻灯片作者:约书亚·布洛赫

他的观点与您的问题相关:

  • “如有疑问,请忽略它”
  • “不要让实现细节“泄漏”到 API 中”
  • “API 设计很困难”,而且“API 设计”是一门高尚且回报丰厚的技艺”
  • “预计会犯错误”

听起来您有一个棘手的问题需要解决 - 但祝您好运!

If it's not part of the abstraction -- then it shouldn't be in the interface. And if it's only used by 1 of 30 implementing classes, then it's definitely not part of the abstraction.

I did a quick google search for 'api design' and the first hit was:

slides of a presentation by Joshua Bloch.

His points that are relevant to your question:

  • "When in doubt leave it out"
  • 'Don't let implementation details “leak” into API'
  • "API design is tough", but also, "API design is a noble and rewarding craft"
  • "Expect to make mistakes"

It sounds like you have a tough problem to solve -- but good luck!

倾其所爱 2024-12-15 08:51:52

听起来您正在遵循实现驱动的设计,而不是用例驱动的设计。通过考虑来电者的观点,您将能够自己回答其中一些问题。我在这篇博客文章中获得了更多详细信息:

http ://theamiableapi.com/2011/08/29/considering-the-perspective-of-the-caller/

干杯,

费伦茨

It sounds like you are following implementation driven design as opposed to use case driven one. You'll be able to answer some of these questions yourself by considering the perspective of the caller. I've got more details in this blog post:

http://theamiableapi.com/2011/08/29/considering-the-perspective-of-the-caller/

Cheers,

Ferenc

○闲身 2024-12-15 08:51:52

各种类的构造函数参数应该是处理中使用的协作者(或配置值)。这就是如何。对于 30 种不同的实现,这些可能会有所不同。如果某些数据库连接需要而不是其他数据库连接,则仅将其作为构造函数参数提供给其中一个。

然后,该接口构成了应该完成的处理的基础。这就是什么

您应该争取一个 API 名称、参数和方法处于同一概念级别的接口。构造函数参数可能处于较低的概念级别。

The constructor arguments to your various classes should be collaborators (or configuration values) used in processing. This is the how. These can vary for the 30 different implementations. If the database connection is required for some and not others, then only supply it as a constructor argument to one.

The interface then forms a basis for the processing should be done. This is the what.

You should strive for an interface where the API name, arguments and methods are at the same conceptual level. Constructor arguments are likely to be at a lower conceptual level.

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