Java 接口目录结构?

发布于 2024-08-19 19:09:47 字数 1432 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

×纯※雪 2024-08-26 19:09:47

接口并不特别需要自己的目录。它们应该放在有意义的地方,就像类应该放在有意义的地方一样。在许多情况下,将它们放在同一个地方可能是有意义的。

Interfaces don't specifically need their own directory. They should be placed where it makes sense, just as classes should be placed where they make sense. In many cases, it may make sense to put them in the same place.

萧瑟寒风 2024-08-26 19:09:47

我见过的一种模式是将接口放在基目录中,然后将实现放在那里的子目录中。

例如,接口可能会放在这里:

com.myproject.data.dao.CustomerDao (some people do ICustomerDao for interfaces, but some don't like that.)
com.myproject.data.dao.ProductDao

实现可能会放在这里:

com.myproject.data.dao.hibernate.HibernateCustomerDao
com.myproject.data.dao.hibernate.HibernateProductDao
com.myproject.data.dao.someotherorm.SomeOtherOrmCustomerDao
etc.

这可能在某些情况下有效,但在其他情况下可能不起作用,但这只是需要考虑的事情。

One pattern I've seen is to put the interfaces in a base directory, then put the implementations in a subdirectory from there.

For example, interfaces might go here:

com.myproject.data.dao.CustomerDao (some people do ICustomerDao for interfaces, but some don't like that.)
com.myproject.data.dao.ProductDao

And the implementations might go here:

com.myproject.data.dao.hibernate.HibernateCustomerDao
com.myproject.data.dao.hibernate.HibernateProductDao
com.myproject.data.dao.someotherorm.SomeOtherOrmCustomerDao
etc.

This might work in some situations, and might not in others, but just something to think about.

画中仙 2024-08-26 19:09:47

由于已经有一些优点,我只想补充一件事:

在某些项目中,我们甚至将所有接口放入一个子项目(maven 模块)中,并将实现放入另一个子项目中。通过这种方式,可以将接口与实现完全分离,并在项目的早期阶段完成接口项目,并将其交付给使用这些接口工作的其他团队。在每个项目中我们都使用相同的包。

一般来说,我会说,你应该将接口和它们的实现分开,方式并不重要,只要你与之保持一致即可。

As there are already some good points, I just wanna add one thing:

In some projects we have even gone so far that we placed all interfaces into one sub-project (maven module) and implementations into another one. This way it was possible to FULLY seperate the interfaces from the implementations and finalize the interface project very early in the project and deliver it to other teams working agains those interfaces. Within each of the projects we used the same packages.

In general I would say, you should seperate interfaces and their implemetations, the way doesnt really matter, as long as you are consistent with it.

写下不归期 2024-08-26 19:09:47

相同的包。用户不应该知道或关心他们正在使用一个界面

Same package. The user should not know or care that they are using an interface

刘备忘录 2024-08-26 19:09:47

无论您想要在哪里,但将接口保留在相同的包和目录结构中绝对没问题。只需查看 java api 即可。如果您选择任何一个包,您会注意到许多包都包含类和接口。有些接口是由同一包中的类实现的,有些则不是。

我认为最糟糕的做法是坚持必须有一个不同的接口目录。我见过 /services 和 /impl 等目录,它们只会弄乱目录结构。在我目前的工作场所,我们雇佣了很多来来去去的承包商,我们的一些项目有多种类型的界面目录。我认为使用单独目录有意义的唯一情况是,如果您打算将接口复制到其他项目(例如 EJB)中,但即使如此,如果您对接口使用共享项目,它们也可以具有相同的包。

所以简短的答案是,在任何你想要的地方,但不要认为你需要将你的类和接口分开。在许多情况下,最好将它们保存在同一个包/目录中。

Wherever you want, but it is absolutely fine to keep interfaces in the same package and directory structure. Just look at the java api. If you select any of the packages you will notice that many contain both classes and interfaces. Some of the interfaces are implemented by classes in the same package, and some are not.

I think the worst practice is to insist that you must have a different directory for interfaces. I have seen directories like /services and /impl and others, which only muck up the directory structure. At my current workplace we employ a lot of contractors that come and go, and some of our projects have multiple types of interface directories. The only time I think it makes sense to use a separate directory is if you plan to copy the interfaces into other projects, say for EJBs, but even then they can have the same package if you use a shared project for the interfaces.

So short answer is, anywhere you want, but don't think you need to separate your classes and your interfaces. In many cases it is preferable to keep them in the same package/directory.

独自唱情﹋歌 2024-08-26 19:09:47

完全没有必要将接口放在同一目录(包)中。如果您的接口具有公共访问权限,那么您可以将其导入任何包中的任何位置。

Its Not at all necessary to place the interface in the same directory(package). If your interface has a public access, then you can import it anywhere, in any package.

狼性发作 2024-08-26 19:09:47

我读到的问题(但它的表述很奇怪)不是接口是否应该在自己的目录中。问题是您是否应该重新创建完整的目录结构(粗体以强调问题标题中的内容),其中一个分支仅包含接口,如下所示:

pureooabstraction/
 |
 |_com/
   |
   |_example/
     |
     |__SomeInterface.java
     |__SomeOtherInterface.java

src/
 |
 |_com/
   |
   |_example/
     |
     |__SomeClass.java
     |__...

pureooabstraction/ 目录结构将仅包含“纯抽象类”(从面向对象的角度来看,而不是Java“抽象”定义),即Java中的接口。

“代码”所在的小实现细节(在 OOA/OOD 级别不存在)将位于 src/ 目录中。

如果您的开发过程从 OOA 到 OOD 再到 OOP,这当然是有意义的。

The question as I read it (but then it's strangely formulated) is not if interface should be in their own directory or not. The question is if you should recreate your complete directory structure (bold to emphasis what is in the question's title) where one branch would contain only interfaces, like this:

pureooabstraction/
 |
 |_com/
   |
   |_example/
     |
     |__SomeInterface.java
     |__SomeOtherInterface.java

src/
 |
 |_com/
   |
   |_example/
     |
     |__SomeClass.java
     |__...

Where the pureooabstraction/ directory structure would contain only "pure abstract classes" (from an OO point of view, not the Java 'abstract' definition), aka interfaces in Java.

And the petty implementation details (which don't exist at the OOA/OOD level) where "code" lies would go in the src/ directory.

It certainly makes sense if your development process goes from OOA to OOD to OOP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文