简体   繁体   English

在Java中,将所有接口提取到单独项目中的技术动机是什么?

[英]In Java, what is the technical motivation for extracting all interfaces into a separate project?

I've seen Java projects where the interfaces are all extracted into a separate project. 我见过Java项目,其中的接口都被提取到一个单独的项目中。

What is the motivation for that? 这是什么动机? Is it just organizational? 它只是组织吗?

For instance, the atmosphere project does this. 例如,大气项目就是这样做的。 I've seen others. 我见过其他人。

I'm considering using it as an organizing principle on a project I'm running and would like to know what other benefits it may provide. 我正在考虑将它作为我正在运行的项目的组织原则,并且想知道它可能提供的其他好处。

There exists one use-case: Java SPI, service provider interface . 存在一个用例: Java SPI,服务提供者接口 The interface(s) are provided separately, and (alternative) implementations come separately. 接口是单独提供的,(替代)实现是分开提供的。 By a manifest entry with interface name, using an interface one can find all/any provider of that interface. 通过具有接口名称的清单条目,使用接口可以找到该接口的所有/任何提供者。 Xalan and Xerces XML implementations come to mind. 我想到了Xalan和Xerces XML实现。

Also without SPI being able to provide several implementations, like a Test Driven Development prototype, may make sense. 此外,没有SPI能够提供多种实现,如测试驱动开发原型,可能有意义。

Just to offer my 2 cents, 只是提供我的2美分,

I'm currently working on a C# project that has a separate project holding all the interfaces. 我目前正在开发一个C#项目,它有一个包含所有接口的独立项目。 The project was named 'framework' and was part of a larger project comprising of 10+ implementations of interfaces from that framework project. 该项目被命名为“框架”,并且是一个更大的项目的一部分,该项目包含来自该框架项目的10多个接口实现。 My guess was (which is also later confirmed by my immediate superior) is that it fully separates implementations from design , something called loose coupling . 我的猜测是(后来我的直接上级证实了这一点)是它完全将实现与设计分开 ,称为松散耦合

That way various projects that inherit from the framework project can be separately developed or swapped out independently. 这样,从框架项目继承的各种项目可以单独开发或独立交换。 From a developer that's new to the project, it's easier for him/her to get acquainted with all the methods that are used in other projects in one central place. 从一个刚接触项目的开发人员,他/她更容易熟悉在一个中心位置的其他项目中使用的所有方法。 Adding new methods or removing old ones are all done in one project, and every other project that uses those interfaces have a strict 'contract' to follow. 添加新方法或删除旧方法都是在一个项目中完成的,并且使用这些接口的每个其他项目都要遵循严格的“合同”。 In essence it assists with maintaining the project in the long run. 从本质上讲,它有助于长期维护项目。

It also makes it easier to create mockups of certain parts of the framework during testing, to isolate each class individually and test them for possible errors. 它还使得在测试期间更容易创建框架的某些部分的模型,以单独隔离每个类并测试它们是否存在可能的错误。

It assists in adhering to the interface segregation principle in which if a particular interface has for example only a 'save' method, but we need 'log' and 'read' for specific implementation classes, we can just create another interface that will inherit from the parent interface, all in the framework project, and no wade around in different projects to find the interface we want to add. 它有助于遵循接口隔离原则,在该原则中,如果特定接口仅具有“保存”方法,但我们需要针对特定​​实现类的“日志”和“读取”,我们可以创建另一个将继承的接口父接口,所有在框架项目中,并没有涉及到不同的项目,以找到我们想要添加的接口。 I found this when researching on Interface Segregation Principle. 在研究接口隔离原理时发现了这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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