Which one should we use? And why?
Modularization is the process of decoupling a software system into multiple modules. In addition to reducing complexity, it increases the understandability, maintainability and reusability of the system. In this article, some of the modularization methods will be mentioned.
Package by Layer
In this project structure, classes are placed in the architectural layer package they belong to. This method causes low cohesion within packages because packages contain classes that are not closely related to each other. Below is an example of packaging by layers:
├── com.app
└── controller
├── CompanyController
├── ProductController
└── UserController
└── model
├── Company
├── Product
└── User
└── repository
├── CompanyRepository
├── ProductRepository
└── UserRepository
└── service
├── CompanyService
├── ProductService
└── UserService
└── util
When we examine this structure, we see that there will be high coupling between packages as well as low cohesion within packages. Since Repository
classes will be used in Service
classes and Service
classes will be used in Controller
classes, high coupling occurs between packages.
Moreover, it is necessary to make changes in many packages to implement the requirements.
“I felt like I had to understand everything in order to help with anything.” — Sandi Metz
What are Cohesion and Coupling?
Cohesion: It refers to the degree of logical relationship of package members to each other. High relationship between members ensures package independence. Low cohesion not only reduces independence but also significantly reduces reusability and understandability.
Coupling: It refers to the degree of interdependence between packages/classes. Low coupling significantly increases maintainability. How? Since changes made within a class due to the requirements will not affect other classes, side effects do not occur and maintenance becomes easie