How should I structure a Java application, where do I put my classes?

后端 未结 10 1341
轻奢々
轻奢々 2020-12-24 05:17

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

相关标签:
10条回答
  • 2020-12-24 05:52

    Short answer: draw your system architecture in terms of modules, drawn side-by-side, with each module sliced vertically into layers (e.g. view, model, persistence). Then use a structure like com.mycompany.myapp.somemodule.somelayer, e.g. com.mycompany.myapp.client.view or com.mycompany.myapp.server.model.

    Using the top level of packages for application modules, in the old-fashioned computer-science sense of modular programming, ought to be obvious. However, on most of the projects I have worked on we end up forgetting to do that, and end up with a mess of packages without that top-level structure. This anti-pattern usually shows itself as a package for something like 'listeners' or 'actions' that groups otherwise unrelated classes simply because they happen to implement the same interface.

    Within a module, or in a small application, use packages for the application layers. Likely packages include things like the following, depending on the architecture:

    • com.mycompany.myapp.view
    • com.mycompany.myapp.model
    • com.mycompany.myapp.services
    • com.mycompany.myapp.rules
    • com.mycompany.myapp.persistence (or 'dao' for data access layer)
    • com.mycompany.myapp.util (beware of this being used as if it were 'misc')

    Within each of these layers, it is natural to group classes by type if there are a lot. A common anti-pattern here is to unnecessarily introduce too many packages and levels of sub-package so that there are only a few classes in each package.

    0 讨论(0)
  • 2020-12-24 05:54

    Where I'm working, we're using Maven 2 and we have a pretty nice archetype for our projects. The goal was to obtain a good separation of concerns, thus we defined a project structure using multiple modules (one for each application 'layer'): - common: common code used by the other layers (e.g., i18n) - entities: the domain entities - repositories: this module contains the daos interfaces and implementations - services-intf: interfaces for the services (e.g, UserService, ...) - services-impl: implementations of the services (e.g, UserServiceImpl) - web: everything regarding the web content (e.g., css, jsps, jsf pages, ...) - ws: web services

    Each module has its own dependencies (e.g., repositories could have jpa) and some are project wide (thus they belong in the common module). Dependencies between the different project modules clearly separate things (e.g., the web layer depends on the service layer but doesn't know about the repository layer).

    Each module has its own base package, for example if the application package is "com.foo.bar", then we have:

    com.foo.bar.common
    com.foo.bar.entities
    com.foo.bar.repositories
    com.foo.bar.services
    com.foo.bar.services.impl
    ...
    

    Each module respects the standard maven project structure:

       src\
       ..main\java
         ...\resources
       ..test\java
         ...\resources
    

    Unit tests for a given layer easily find their place under \src\test... Everything that is domain specific has it's place in the entities module. Now something like a FileStorageStrategy should go into the repositories module, since we don't need to know exactly what the implementation is. In the services layer, we only know the repository interface, we do not care what the specific implementation is (separation of concerns).

    There are multiple advantages to this approach:

    • clear separation of concerns
    • each module is packageable as a jar (or a war in the case of the web module) and thus allows for easier code reuse (e.g., we could install the module in the maven repository and reuse it in another project)
    • maximum independence of each part of the project

    I know this doesn't answer all your questions, but I think this could put you on the right path and could prove useful to others.

    0 讨论(0)
  • 2020-12-24 06:05

    One thing I've done in the past - if I'm extending a class I'll try and follow their conventions. For example, when working with the Spring Framework, I'll have my MVC Controller classes in a package called com.mydomain.myapp.web.servlet.mvc If I'm not extending something I just go with what is simplest. com.mydomain.domain for Domain Objects (although if you have a ton of domain objects this package could get a bit unwieldy). For domain specific constants, I actually put them as public constants in the most related class. For example, if I have a "Member" class and have a maximum member name length constant, I put it in the Member class. Some shops make a separate Constants class but I don't see the value in lumping unrelated numbers and strings into a single class. I've seen some other shops try to solve this problem by creating SEPARATE Constants classes, but that just seems like a waste of time and the result is too confusing. Using this setup, a large project with multiple developers will be duplicating constants all over the place.

    0 讨论(0)
  • 2020-12-24 06:06

    Class names should always be descriptive and self-explanatory. If you have multiple domains of responsibility for your classes then they should probably be refactored.

    Likewise for you packages. They should be grouped by domain of responsibility. Every domain has it's own exceptions.

    Generally don't sweat it until you get to a point where it is becoming overwhelming and bloated. Then sit down and don't code, just refactor the classes out, compiling regularly to make sure everything works. Then continue as you did before.

    0 讨论(0)
  • 2020-12-24 06:06

    One thing that I found very helpful for unit tests was to have a myApp/src/ and also myApp/test_src/ directories. This way, I can place unit tests in the same packages as the classes they test, and yet I can easily exclude the test cases when I prepare my production installation.

    0 讨论(0)
  • 2020-12-24 06:11

    I like break my classes down into packages that are related to each other.

    For example: Model For database related calls

    View Classes that deal with what you see

    Control Core functionality classes

    Util Any misc. classes that are used (typically static functions)

    etc.

    0 讨论(0)
提交回复
热议问题