Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package-by-layer VS package-by-feature library naming?

I know this has several associated posts, but I have some specific questions which I am hoping I can get help with. Sorry if they are very basic..

Here is a sample problem - very simplified, but you get the picture - I have several objects, which have some common function e.g. departments in a pharmaceutical company - neurology, oncology, infection etc. They all need to parse patient document files, and upload data to the database. Of course the nature of the data is slightly different for each department. If I used package-by-feature, I would have

com.company.neurology
      Neurology.java
      NeurologyDocument.java
      NeurologyDAO.java

com.company.infection
      Infection.java
      InfectionDocument.java
      InfectionDAO.java

etc. The problem is that I need an abstract class that the Document classes need to extend e.g.

AbstractDocument.java
public class AbstractDocument
{
      public void validateDocument(){...}
      public void readDocumentHeader(){...}
      public void readDocumentFooter(){...}
       ...
}

Some data access files e.g.

DBConnection.java
    public class DBConnection
    {
        public void makeConnectionToDB() {... }
        public void createCache() {... }
        public void closeConnectionToDB() {... }
    }

some error classes

ParseError.java, PatientNotFoundException etc.

If packages are by feature, where do these common classes/interfaces go?

like image 213
user2689782 Avatar asked May 06 '15 16:05

user2689782


People also ask

What is packaging by layer?

A very popular approach for a project structure is to package by layer. This leads to a package for each technical group of classes. Packaging by layer groups all classes by a technical point of view. Let's add the call hierarchy to the picture to “clearly” see which class depends on which class.

Why package by feature?

Advantages of Package by Feature — As mentioned, Package by Feature has packages with high cohesion, low coupling and high modularity. — Package by Feature allows some classes to set their access modifier package-private instead of public , so it increases encapsulation.

Which of the given statement is not true about a Java package?

6. Which of the following is an incorrect statement about packages? Explanation: A package can be renamed only after renaming the directory in which the classes are stored.

Which of the following is true about packages?

Which of the following is true about packages? Explanation: A library contains many packages and it is used to collect and describe elements that can be shared globally among all the design units. It may contain any commonly used data type, functions or constants.


1 Answers

I do not think in your case you should create some common package. I guess something is wrong with the abstractions used.

You must find a way to unite classes with the same purpose into some package and place this package under com.company.shared.<feature_name>.

In your case it could be:

  • com.company.shared.database
  • com.company.shared.data_parsing

It is no problem for a package to contain only one or two classes. But it should unite only classes with the same purpose and have a clear speaking name.

Avoid such names for both packages and classes as common. A name should talk for itself.

like image 72
Vladislav Rastrusny Avatar answered Oct 02 '22 13:10

Vladislav Rastrusny