Advantages and disadvantages of organizing packages in Java projects
As the project I'm working on gets bigger and bigger, I'm beginning to be very uncertain about subcontracting classes Suppose the project has many layers, in which there are interfaces and implementations, and even more sub layers (components) I always end up getting a lot of packaging, and it's starting to get a little messy
So I want to know how others use packages Do you like courses with many courses or few courses? Do you prefer to separate the implementation from the interface? Wait... Generally speaking, your daily habits of using the package and the advantages / disadvantages of your method
Thank you for your post
Solution
The package is designed to help you find something
If they make it more chaotic, you're not doing it right If the package structure is not intuitive, it is actually more difficult to find analogy in plane structure
As far as I know, there are two basic school courses:
>First, organize by module Here, your higher-level software package is a different module of the system. You can further split it through functions. > Organize by function Here, you first organize by function (for example, all controller classes in one package, all data containers in another package, etc.), and you can choose to subdivide by module
Both systems have advantages and disadvantages, and I find them very similar, although I prefer the modular approach
It's really important to follow one system instead of mixing it with another If your new course doesn't seem to belong to your existing course, please don't turn your course into a course they don't belong to, and don't be afraid to create a new course
If the package looks too big, you may want to split it However, the decision on whether to split the package should consider whether there is a clear conceptual division between the categories rather than numbers A package containing one class is as good as a package with thirty classes if they are obviously why they are there
As for separating interfaces and implementations, first of all, I may question their necessity I often encounter interfaces with only one reasonable implementation, which makes me question the reason for their existence (sometimes there are good reasons, but often not.)
But if you have multiple implementations of a given interface, yes, I separate them The interface will be com foo. Bar, the implementation will be similar to com foo. bars. CrowBar,com. foo. bars. TaskBar. Obviously, if your implementation belongs to a different module, you can change it to com foo. moduleX. bars. Crowbar or com foo. bars. moduleX. Crowbar, depending on the system you follow
Re reading all this sounds a little complicated, but perhaps the first sentence is the most important: don't blindly follow the packaging principles. Packaging should help you find the course, not hinder you