What should be added to the project, folder and package

I am starting with Java. Now I am using Eclipse. What confuses me is that I do not understand the dogma of entering files into a project, folder, and package. If I create a new class, should I put it in the source package or create a new package or a new folder or even a new project and why?

+4
source share
2 answers

It completely depends on which application you are developing. If you are developing a large application with many modules, you must create different projects related to each other.

But you say that you are a beginner, so the answer to your question will be as follows: you should always classify your classes in different packages. Classification is your choice - you can even classify alphabetically if it suits you. I usually classify them according to their functionality, as all classes of my controller will be in one package, as well as my Bean classes, listeners, constants, assemblers, utilities, parsers, comparators, models, visualization tools, etc.

This Java Practices article will be helpful.

+3
source

Project structure

I highly recommend that you follow the Maven rules for the structure: http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html They are the result of many years of experience.

However, designing such a structure is always a compromise between extensibility and simplicity. The more you foresee, the more difficult it will be to maintain, but the more you return your investment, the faster it will grow.

To be pragmatic, and even I'm not very convinced (because my job is to anticipate;)), try the KISS philosophy: http://en.wikipedia.org/wiki/KISS_principle .

Package structure

As Amandeep Jiddewar said, packages should present their own functionality. At the very least, they should be reverse domains, such as: http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html . For example, the stackoverflow API should be: com.stackoverflow. *

And maybe for the first time they immersed it in an MVC pattern

  • com.stackoverflow.controler
  • com.stackoverflow.model
  • com.stackoverflow.view

etc.

And when the project was mature ... or grew too much, they divided it into war for the presentation layer and jar (s) for the service level ...

Further considerations

There is no special rule. As I said, you always have to deal with a compromise. Only experience will give you the opportunity to find out when you need to change the configuration.

Look around for more ideas:

+1
source

Source: https://habr.com/ru/post/1432847/


All Articles