Do you know about “Package by layer and Package by feature” ?

Chaudhary Vivek Kadiyan
4 min readMar 2, 2024

--

Hello dear friends , Hope you all are doing great in your job .

Today in this blog we are going to discuss another interesting topic , how should you package your code for better readability and easy maintainability . So with out any delay

Let’s Get Started

“Spring Boot Application”

Why Packaging is so Important

i) Reducing Complexity
ii) Better Understanding
iii) Easy to Maintain
iv) Decoupling

Packaging of any application plays a very important role, specially if you are working on very large applications , so depending on the requirement of your project you can choose any of these two most popular methods.

Packaging by Layer

We all know in any project we have different different layer like controller , service , repository or DAO , helpers , util etc. This separation of concerns makes the codebase more modular, easier to understand, and maintain. It also facilitates testing and promotes code reuse, as each layer has well-defined responsibilities and dependencies.

For Example : -


com.application.controller
ApplicationController.java
ConsumentController.java
ProductHistoryController.java
ReportController.java

com.application.service
ProductMasterService.java
ProductReportManagementService.java

com.application.repository
ProductHistoryRepo.java
ProductMasterRepo.java
com.application.entiry
ProductHistoryEntity_1.java
ProductHistoryEntity_1.java
ProductHistoryEntity_1.java

So as in above picture we have different layer , each layer has it’s own importance to perform specific purpose and in this type of packaging no class is directly related to each other , each has it’s own role as per it’s layer where it belongs to.

What is the main problem with above approach is , packages having high coupling “why is it so” , because controller classes will use service , service classes will use helper , repository and utility so each class highly coupled up which is not at all seems good way or good design as per system design principle .

What a good approach says — — — — — — — — — →>>>>>>>>>

“High Cohesion and low coupling between packages are always a better system design approach.”

How can we make above possible and………….. then the second approach comes in picture which name is “ Packaging by feature

Package by Feature

As Name implies when you are packaging your code with the feature . Ah!!!!!!

What do you mean by that no worries i will tell you …………Suppose you have a microservice Product Management in which you have features like

i) Product Order Report Management
ii) Product Orders History Per Customer
iii) Product User Access Management
iv) Product Master Data Management

So for above we will have to create a spring boot application with above feature in which we will create different classes like services, entities, helpers and repositories but how will you package those under this package with feature design that you can see in following snippet…..

i)  ProductOrderReportManagement Feature will package as
com.product.report
a) ReportController.java
b) ReportService.java
c) ReportHelper.java
d) ReportUtil.java
e) ReportRepository
f) ReportEntityClass.java

ii) ProductOrdersHistoryPerCustomer Feature will package as
com.order.history
a)OrderHistoryController.java
b)OrderHistoryService.java (interface)
d)OrderHistorySeerviceImpl.java (implementation class)
e)OrderHistoryRepository.java
f) OrderHistoryEntity_1.java
g) OrderHistoryEntity_2.java

iii) ProductUserAccessManagement
com.user.management
a) UserManagementController.java
b) UserManagementService.java
c) UserManagementServiceImpl.java
d) UserManagementEntity.java
e) UserManagementRepository.java
f) UtilityForUserManagement.java
g) UserManagementHelper.java

iv) ProductMasterDataManagement
com.product.master
a) ProductMasterController.java
b) ProductMasterEntity_1.java
b) ProductMasterEntity_3.java
c) ProductMasterEntity_4.java
d) ProductMasterService.java
f) ProductMasterRepository.java

In this approach you can see each class structure is roaming around in its package with it’s feature so automatically there will loose coupling and high cohesion.

Points to remember

Above packaging is giving better readability and maintainability comparatively to the first approach.

Another benefit is , you have better control on access modifiers , what does it means , it means your all service classes , repo classes, entity classes under a same package so no need to create unnecessary public classes.

One Another benefit which i see along with above , here we are seeing our application feature wise packaged so giving more business clarity and that will give more confidence to meet the client objective or business requirement.

In small applications you can go with layer approach but for bigger functionality oriented applications by feature always a recommended choice to implement.

That’s it for today , let’s meet in another blog with another interesting topic.

If you feel this content is meaning full for you so with out any delay subscribe and clap for the content.

Thanks you so much , share this with you friends.

--

--