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

查看:24
本文介绍了究竟什么是“基于接口的编程"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常听到/读到基于接口的编程,但我不太清楚这到底意味着什么.基于接口的编程是一个实际的独立主题,实际上有关于它的书籍吗?如果是这样,有人可以推荐任何好的吗?

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?

我在阅读有关如何设计优秀 API 的文章时遇到了基于接口的编程,并想了解更多相关信息.现在我不清楚如何正确地围绕接口设计 API.

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.

非常感谢任何信息.

推荐答案

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

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).

这意味着:

  • 您可以在实现真正的依赖之前编写代码
  • 您可以非常轻松地通过模拟进行测试(无需模拟类,这会变得很丑)
  • 很明显,您依赖 API 而不是实现(即您的耦合更松散)

这篇关于究竟什么是“基于接口的编程"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆