使用界面时的最佳实践

发布于 2024-11-02 20:36:08 字数 176 浏览 7 评论 0原文

在设计界面时,我经常遇到同样的情况。这种情况是使用接口的某些实现需要接口中的特定参数,而其他实现则不需要。

  • 设计界面时的最佳实践是什么?
  • 某些实现可以实现接口但不使用所有参数吗?

或者在这些情况下,我应该只接受参数列表(某些结构)并在每个实现中相应地处理该列表?

Many times when designing interfaces I keep running into the same situation. The situation is where certain implementations using an interface require particular parameters in the interface while others do not.

  • What is the best practice when designing an interface?
  • Is it OK to have certain implementations that implement the interface but not use all the parameters?

Or in these situations should I just be taking in a list (some structure) of parameters and deal with that list accordingly in each implementation?

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

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

发布评论

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

评论(2

你的心境我的脸 2024-11-09 20:36:08

不,这不行。它打破了里氏替换原则。

在我看来,你的界面试图做的事情太多了。要么使用接口继承,要么将接口拆分为多个接口。请注意,拥有许多小接口比拥有一个大接口更好。类仍然可以实现所有这些。

接口和类一样,应该遵循 SRP(单一职责原则)。恕我直言,接口这样做要重要得多,因为它们将设计强加于应用程序的其余部分。

我还倾向于尽量避免从接口添加属性。

No it's not OK. It breaks Liskovs Substituion Principle.

Sounds to me that your interfaces are trying to do too much. Either use interface inheritance or split the interface into multiple ones. Do note that it's better having many small interfaces than one large. Classes can still implement all of those.

Interfaces, like classes, should follow SRP (Single Responsibility Principle). imho it's much much more important that interfaces do so since they force a design upon the rest of your application.

I also tend to try to avoid adding properties as much as possible from interfaces.

时常饿 2024-11-09 20:36:08

在某些情况下是可以的。只要它满足接口承诺维护的契约,您实际上对实现中的参数做什么并不重要。

但是您应该重新考虑您是否实际上不需要为某些需要这些参数的事物提供更具体的接口。在抽象堆栈中,具有“较低”接口需要“较高”参数是封装的破坏。

It's ok in some cases. It doesn't really matter what you actually do with the parameters in the implementation as long as it satisfies the contract that the interface promises to uphold.

But you should reconsider whether you don't actually want a more specific interface for some things that need those parameters. In your abstraction stack, having a "lower" interface need "higher" parameters is a break of encapsulation.

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