简体   繁体   中英

what is the difference between required and Provided interfaces

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 ?

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.

在此处输入图片说明

On the implementation level a provided interface is the interface implemented by a class (in the most common sense, eg a class B implements the interface I). Required interface would be any use of an interface by a component (eg 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).

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.

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)

One other term you might encounter is SPI Service Provider Interface which 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.

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 .

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

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

An interface can be considered a contract between two agents or components. The concept can be applied to the contents of a class, a program in a container interacting via network endpoints and many other systems. To execute a transaction conform to the contract, one agent requires a service or result from the other, and the other agent or component provides a result to the agent or component based in the request input. The interface is based on an agreement between bot agents. The basis of the contract defines a finite number of information objects to be transferred from the requesting agent to the match the specifications of the service providing agent or component. The providing agent produces a resulting object or result by means of a process that is based on the objects provided by the requesting agent. The result is transferred to the requesting agent by sendingan object, a message or transferring data or perhaps completing an action that changes a state. The requesting part or component manipulates the required interface. The service providing agent or component, manipulates the provided interface to transfer a resulting message. Note that the result maybe deterministic (we want that in most of of our design user cases) but the service could also have a stochastic response, either because there is uncontrolled noise perturbing the process that leads to the resulting object or action, or because the process is intrinsically stochastic, as in quantum-mechanical systems.
Classes, methods or functions can always be considered as elements of an interface having a required side (the call, transferring the call parameters) matching a provider interface, the function or the method which gives the response. But the concept can be applied to describe the interaction of any kind of signaling systems with interfaces between components conforming to an interface contract model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM