How to sort related classes from different projects
I have a situation where I refactor old code, separating the old project from the monsters and breaking it down (for various reasons) into smaller sub-projects. One project will end up containing mostly interfaces, while their associated implementations are in another project, and I'm not sure about the best way to customize the package structure.
Should I go for
org.company.interfaceproject.util.InterfaceClass and
<i> org.company.implementationproject.util.ImplementationClass
or
org.company.project.util.InterfaceClass and
<i> org.company.project.util.ImplementationClass
where the first implementation has the advantage of indicating which project the files belong to, and the second is not confused with the fact that the files are in different projects at all.
I'm guessing there is no right or wrong here, but I'm curious if anyone has any opinions on this.
a source to share
Yes, you just need to come up with a naming convention. Usually a combination of both is appropriate for our company to avoid ambiguity. For example, let's say you have an interface:
org.company.service.UserService
We would then use the following for the implementation class that was linked to or dependent on spring:
org.company.service.spring.UserServiceImpl
In this case, the best of both viewpoints:
- You have classes in a separate package
- Using this naming convention for this class, he sees its implementation
UserService
, and is still distinguishable even when both packages are imported.
a source to share
Both have merits. It ultimately depends on your intentions for the project. If your intent is to eventually create alternative implementations of the interfaces, it might make more sense with Option 1. If it is the only implementation of the interfaces, Option 2 would make more sense.
a source to share
Sun has a Naming Convention . For packages:
The unique package name prefix is always written in ASCII with lowercase letters and must be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the two English languages defined by the countries specified in the ISO 3166 standard , 1981.
Subsequent components of the package name vary according to the organization's own internal naming conventions. Such conventions may indicate that some of the components of a directory name are divisions, departments, projects, machines, or logins.
So, I would prefer the second option where you provide the project name. Or I would combine both types:
org.company.project.interfacepackage.util.InterfaceClass and
org.company.project.implementationpackage.util.ImplementationClass
a source to share
Mostly agree with Clinton.
Ultimately, each package name is an island before Java, but it can be convenient to split things up according to what is compiled with what was at build time, for example:
com.foo.client. * com.foo.server. * com.foo.common. *
This basically simplifies the ant fileset. Note that this applies even if the location of the source files is completely different, due to how things are created or something else. The only thing I would like to say is to be careful not to get the same package in multiple source directories! It can be ugly and easy to do by accident.
So, if this mindset doesn't push you to create separate high-level packages, I like the style of putting an implementation package inside an interface package, giving them a package name that suggests how it specializes, and naming the implementation names FooImpl
. You almost never need to import multiple implementations, but sometimes you want to import both an interface and an impl, in which case it's nice if they have a similar name.
a source to share