究竟什么是 "基于接口的编程"?[英] What exactly is "interface based programming"?

本文是小编为大家收集整理的关于究竟什么是 "基于接口的编程"?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我经常听到/阅读有关基于交互的编程的/阅读,但我并不完全清楚这意味着什么.是否基于接口的编程是一个实际的独立主题,实际上写了关于它的书籍?如果是这样,谁能推荐任何好的?

我遇到了基于接口的编程,因为我正在阅读有关API的设计方式,并希望更多地了解它.现在,我尚不清楚如何正确地设计界面周围的API.

任何信息都非常感谢.

推荐答案

基本上是用接口而不是具体类(或更糟糕的是静态方法)来表达您的依赖关系的问题.因此,如果您的一类需要执行身份验证,则应提供IAuthenticator(或其他).

这意味着:

  • 您可以在实施实际依赖性之前编写代码
  • 您可以非常轻松地通过嘲笑测试(不必嘲笑课程,这变得丑陋)
  • 很明显,您在API而不是实现方面依赖什么(即您有更松散的耦合)

其他推荐答案

"实用的API设计"第6章Jaroslav Tulach 标题为"针对接口而不是实现的代码".它解释说,通过针对界面而不是特定的实现进行编码,您可以将模块(或组件)分解系统中,从而提高系统质量.

bertrand meyer in oosc2 清楚地解释了为什么"为什么"关闭"系统并使其更模块化提高其质量.

其他推荐答案

查看这些非常受欢迎的讨论是否有帮助:

最好的类比是帮助非木板开发人员基于界面的编程吗?

我为什么要使用接口?

何时需要接口?

什么时候应该使用接口?

接口的目的是什么?

接口的好案例

"编程到接口"意味着什么?

本文地址:https://www.itbaoku.cn/post/627626.html

问题描述

I often hear/read about interfaced based programming but I am not exactly clear on what that really means. Is interfaced based programming an actual stand alone topic that actually has books written about it? If so, can anyone recommend any good ones?

I came across interface based programming as I was reading about how good APIs are designed and would like to learn more about it. Right now I am not clear how to properly go about designing an API around interfaces.

Any info is greatly appreciated.

推荐答案

It's basically a matter of expressing your dependencies in terms of interfaces instead of concrete classes (or worse, static methods). So if one of your classes needs to perform authentication, it should be provided an IAuthenticator (or whatever).

This means that:

  • You can write your code before implementing the real dependency
  • You can test via mocking really easily (without having to mock classes, which gets ugly)
  • It's clear what you depend on in terms of the API instead of implementation (i.e. you have looser coupling)

其他推荐答案

Chapter 6 of "Practical API Design" by Jaroslav Tulach is titled "Code Against Interfaces, Not Implementations". It explains that, by coding against an interface rather than a specific implementation, you can decouple modules (or components) in a system and therefore raise the system quality.

Bertrand Meyer in OOSC2 explains clearly why "closing" a system and making it more modular raises its quality.

其他推荐答案

See if these very popular discussions help:

What is the best analogy to help non-oop developers grok interface based programming?

Why would I want to use Interfaces?

When are interfaces needed?

When should one use interfaces?

What is the purpose of interfaces?

Good Case For Interfaces

What does it mean to “program to an interface”?