Java 必需的和提供的接口之间有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23456112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:05:34  来源:igfitidea点击:

what is the difference between required and Provided interfaces

javaoopinterfaceumlmodeling

提问by Micha agus

I know in general :

我一般知道:

an interface is a reference type, it is similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces ?

接口是一种引用类型,它类似于一个类,只能包含常量、方法签名、默认方法、静态方法和嵌套类型。方法体仅存在于默认方法和静态方法中。接口不能被实例化——它们只能由类实现或由其他接口扩展?

But what is the difference between required and Provided interfaces ?

但是 required 和 Provided 接口之间有什么区别?

回答by Duncan Jones

Required and provided interfaces appear to be UML-related terms, where a provided interface describes functionality offered by a class and required interfaces describe functionality needed by another class: further reading.

所需和提供的接口似乎是与 UML 相关的术语,其中提供的接口描述一个类提供的功能,而所需的接口描述另一个类所需的功能:进一步阅读

In Java, all interfaces are the same; there is no distinction between provided/required.

在 Java 中,所有接口都是相同的;提供/要求之间没有区别。

Previous link no longer work, but https://www.ibm.com/developerworks/rational/library/dec04/bell/index.htmlcan be of help

以前的链接不再有效,但https://www.ibm.com/developerworks/rational/library/dec04/bell/index.html可以提供帮助

回答by Aleks

Provided and required interface always refer to the concept of interface, indicating the point of view.

提供的和需要的接口总是指接口的概念,表明观点。

I hope the following diagrams sheds some light on the subject.

我希望下面的图表能对这个主题有所了解。

enter image description here

在此处输入图片说明

On the implementation level a provided interfaceis the interface implemented by a class (in the most common sense, e.g. a class B implements the interface I). Required interfacewould be any use of an interface by a component (e.g. if a class A defines a method that has the interface I as a parameter, this means that class A has a required interface I).

在实现层面上,提供的接口是由类实现的接口(在最常见的意义上,例如类 B 实现了接口 I)。必需的接口可以是组件对接口的任何使用(例如,如果类 A 定义了一个以接口 I 作为参数的方法,这意味着类 A 具有必需的接口 I)。

回答by Hurda

I think that you are confusing interface in general sense and language specific construct that is also called interface.

我认为你混淆了一般意义上的界面和语言特定的结构,也称为界面。

In general sense interface means point of intreaction between two parts/objects/system. At very low level, you can say that all public members (methods + fields) of an object compose its intreface.

一般意义上的接口意味着两个部分/对象/系统之间的交互点。在非常低的层次上,您可以说一个对象的所有公共成员(方法 + 字段)组成了它的接口。

At higher abstraction level programmers often think about API as interface for library/system. But that does not mean that this API consist of just one Java interface. The API contains all objects, methods, contsructors, config files... that are ment to be used by users of the library.That is probably what is ment by your required and provided interfaces.

在更高的抽象级别,程序员通常将 API 视为库/系统的接口。但这并不意味着这个 API 只包含一个 Java 接口。API 包含所有对象、方法、构造函数、配置文件......这些都是由库的用户使用的。这可能是你需要和提供的接口。

If you write java libary, you usually require the API of Java standard library (everything in java package) - that would be the required interface. (it can be provided by JVM of any implementation, for example Android is using the same interface as Java but it is not java) On the other end your library would also expose some interface - the way people could use your library - that would be called the provided interface. (again if I say interface I don't mean one java interface, it would be probably mix of several interfaces + implementations + some value classes)

如果您编写 java libary,您通常需要 Java 标准库的 API(java 包中的所有内容)——这将是所需的接口。(它可以由任何实现的 JVM 提供,例如 Android 使用与 Java 相同的接口,但它不是 Java)另一方面,您的库也会公开一些接口 - 人们可以使用您的库的方式 - 那将是调用提供的接口。(同样,如果我说接口,我不是指一个 Java 接口,它可能是几个接口 + 实现 + 一些值类的混合体)

One other term you might encounter is SPI Service Provider Interfacewhich is similar to API, but the users of SPI don't make calls to this interface, but rather implement it and expose it back to the original system. It's a way to describe interface for plugins.

您可能会遇到的另一个术语是 SPI服务提供者接口,它类似于 API,但 SPI 的用户不会调用此接口,而是实现它并将其公开回原始系统。这是一种描述插件接口的方式。