Search code examples
javajakarta-eemodels

Java packaging conventions - what about models?


I started in PHP, where packages aren't required. We're going to be building a project at my work that is in Java and I'm considering the architecture. The system will be a content management system with 3 levels. We'll have various models for all different things. Should all the models be in one package, should they be broken up into various packages grouped with all the other functionality pertaining to their "section" or something else completely?


Solution

  • There are no absolute rules for this. As a rule of thumb I'd say that a package should contain no more than 10 and no less than 2 compilation units, but tastes differ. Try to find a taxonomy that lets you divide the models into several packages without feeling artificial.

    But I would not put the models into the same package as code that works with them. Try to make your package names representative of what happens in them, e.g.

    com.myapp.model.x
    com.myapp.model.y
    com.myapp.service.x
    com.myapp.service.y
    com.myapp.controller.x
    com.myapp.controller.y