接口应该放在一个单独的包中吗? [英] Should interfaces be placed in a separate package?
问题描述
我是一个从事相当大项目的团队的新手,拥有大量组件和依赖项。对于每个组件,都有一个 interfaces
包,其中放置了该组件的公开接口。这是一个好习惯吗?
I'm new to a team working on a rather large project, with lots of components and dependencies. For every component, there's an interfaces
package where the exposed interfaces for that component are placed. Is this a good practice?
我的惯常做法一直是接口和实现在同一个包中。
My usual practice has always been interfaces and implementations go in the same package.
推荐答案
放置界面和实现是很常见的,并且似乎不是问题。
Placing both the interface and the implementation is common place, and doesn't seem to be a problem.
以示例为例Java API - 大多数类都将接口及其实现包含在同一个包中。
Take for example the Java API -- most classes have both interfaces and their implementations included in the same package.
以 java.util
包:
它包含接口等as 设置
,地图
,列表
,同时还拥有实现,例如 HashSet
, HashMap
和 ArrayList
。
It contains the interfaces such as Set
, Map
, List
, while also having the implementations such as HashSet
, HashMap
and ArrayList
.
此外,Javadocs设计为在这些条件下运行良好,因为它将文档分为 Interfaces 和 Classes 显示包内容时的视图。
Furthermore, the Javadocs are designed to work well in those conditions, as it separates the documentation into the Interfaces and Classes views when displaying the contents of the package.
只有接口的包实际上可能有点过分,除非有大量的接口。但是为了这样做而将接口分成他们自己的包听起来像是不好的做法。
Having packages only for interfaces may actually be a little bit excessive, unless there are enormous numbers of interfaces. But separating the interfaces into their own packages just for the sake of doing so sounds like bad practice.
如果需要区分接口的名称和实现,可以有一个命名约定,使接口更容易识别:
If differentiating the name of a interface from an implementation is necessary, one could have a naming convention to make interfaces easier to identify:
-
使用
我
。这种方法与.NET框架中的接口一起使用。可以很容易地告诉IList
是列表的接口。
使用 - able
后缀。这种方法经常出现在Java API中,例如 Comparable
, Iterable
和 Serializable
等等。
这篇关于接口应该放在一个单独的包中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!