Help with best package structure

I’m making a game with Vaadin, and as I create more and more components I feel like my package structure is getting more and more ugly. I was hoping someone here would be willing to theorize on good package structure guiding principles for a Vaadin project.

I started out mostly organizing things by what type of component they represented, and to an extent I believe this served me well. For example, I have the top-level “page” component, and a page is composed of various panels. Each panel has components inside it. So at first I just had three packages, “Pages,” “Panels,” and “Components.”

But then I started to have to make a lot of things that didn’t really fit in that structure, like a “TabBox” to put the “panels” as tabs inside of, and a Tutorial Window with a bunch of its own stuff, and other modal windows for other things, listeners that were being shared by more than one component, etc.

I think maybe I should try segregating my packages by the top level component it is associated instead of by type of component. So if I have a GameMapPanel, all the components it uses are in its package, and a NewsPanel has all its components in a separate package with it. This will mean I have a lot of packages, some of which are pretty small, and I’d need some kind of misc package for components with a lot of reusability, but I think at least the organization will make more sense than throwing all my buttons in a bag and calling them organized.

What do you guys think? Package by component-type, or package by component associations, or a third option I haven’t thought of?

Hi,

Personally, I’d primarily package stuff based on functionality, not implementation. Then, depending on complexity and number of classes, I’d break it down into implementation sub packages - but only if necessary. If there’s common stuff, I’d stick it a common package - again, depending on complexity & number of classes

e.g.

com.example.mygame.common.component
com.example.mygame.common.page
com.example.mygame.common.panel
com.example.mygame.player.selection
com.example.mygame.player.configuration
com.example.mygame.map
com.example.mygame.news
com.example.mygame.scoreboard
com.example.mygame.tutorial

But really, it’s totally a matter of taste. I’m not sure lots of small packages is a problem at all - assuming you’re not a vim/notepad masochist, IDEs take care of creating folders/files/packages for you - as well as refactoring when you want to move stuff around.

Ultimately, though, the only people that it affects are the people that need to read and maintain the code - and I’m assuming that’s you!

HTH a little,

Cheers,

Charles.

Thanks for the comments and thoughts!

I definitely agree with you. Sorting by functionality and having a common package that’s sorted by component type will probably be the best decision. I guess I have some eclipse-aided refactoring to do!