First of all, I know how to build a Java application. But I have always been puzzled about where to put my classes. There are proponents for organizing the packages in a str
Use packages to group related functionality together.
Usually the top of your package tree is your domain name reversed (com.domain.subdomain
) to guarantee uniqueness, and then usually there will be a package for your application. Then subdivide that by related area, so your FileStorageStrategy
might go in, say, com.domain.subdomain.myapp.storage
, and then there might be specific implementations/subclasses/whatever in com.domain.subdomain.myapp.storage.file
and com.domain.subdomain.myapp.storage.database
. These names can get pretty long, but import
keeps them all at the top of files and IDEs can help to manage that as well.
Exceptions usually go in the same package as the classes that throw them, so if you had, say, FileStorageException
it would go in the same package as FileStorageStrategy
. Likewise an interface defining constants would be in the same package.
There's not really any standard as such, just use common sense, and if it all gets too messy, refactor!