Layered architecture
Layered architecture means that we spread our code across multiple layers doing different tasks. What are the advantages of making code in layers?
Simplicity
Since we spread our code across the layers and different classes representing the layer, our application and the classes themselves will be very simple, small and easy to understand.
Separation of concerns
There will be clear separation of concerns. E.g. the presentation layer will present the data, service layer will have business logic and data access layer manages the data source and retrieving the data for the application, and integration layer responsible for communicating or integrating with other applications. So, each layers have their own function or concerns.
Easy Maintenance
Once our application goes to production, if we have any issue with our application, we can just jump into a layer according to what the problem is. The separation of concern makes clear division between the code
- If database is causing some problems, investigate data access layer.
- If Business logic is to be changed or it is creating some issues, we can easily look to service layer.
- If we have some issues with how data is presented to the user, we can directly investigate presentation layer.
Four layers of application and corresponding Classes in Microservices
Any java enterprise application have multiple layers built in them for easy code management and segregation of concerns. These layers are made up of Java interfaces and classes that we create
Data access layer (DAO)
Independent layer, responsible for performing the CRUD (Create, Read, Update, Delete) operation against the data store or databases.
Service layer
Services provided by the Data access layer is used by the service layer to perform any business logic in the application. All the business logic will reside in the service layer
Presentation layer
Services provided by the service layer is used by presentation layer. It presents the data in appropriate format to the end users.
Integration Layer
Services provided by the service layer is also used by the integration layer. It access other’s APIs or exposes one’s APIs to others. It integrates the application with another service using API and other third-party services. Other applications can also use integration layer to communicate with current system. Integration layer is typically made up of web services.
There are 9 or 10 important interfaces and classes that falls under these layers.
Type |
Filename |
Functions |
Layer |
1) Model class / Entity Class (C) |
___.java Address.java Student.java |
Represent our domain model or Entity model. It typically uses the database model or table structure. |
|
2) DAO Interface |
I___DAO(I) IStudentDAO.java |
Interface for accessing data |
Data Access |
| 3) DAO Implementation |
___DAOImpl (C) StudentDAOImpl.java |
Implementation of data access, extending DAO interface. |
Data Access |
| 4) Service Interface |
I___Service(I) IStudentService.java |
Interface for applying business logic |
Service |
| 5) Service Implementation |
___ServiceImpl (C) StudentServiceImpl.java |
Extends Service interface and implements the business logic for that specific application |
Service |
| 6) Controller |
___Controller(C) StudentController.java |
Extends Service Interface and presents data to end users |
Presentation |
| 7) Utility Class |
Utility(C) |
Perform utility operations across the layers |
Some or all |
| 8) Validator Class |
Validator(C) |
Validate the data that comes from the user and goes to the user |
|
| 9) Service provider/ Service Consumer |
Service provider (C) Service Consumer (C) |
Web services exposed to the third party or accessing web services of third party. Mostly RESTful services |
Integration Layer |
| 10) Special View |
View (C) |
Special view that presents the data as excel sheet or pdf or similar. |
|
Layer wise Technologies
Data Access Layer
This layer is used to connect to underlying database. We use ORM (Object Relational Mapping) in data access layer. Which in turn uses JDBC (Java Database Connector). In modern software we don’t use JDBC directly. Hibernate is a ORM technology that we mostly use with Spring Boot. Spring Data dependency makes it very easy to use this.
Services Layer
Services classes are made with plain java interfaces and classes. We write business logic using plain java code. We don’t need any framework there. But it might require third-party dependencies according to the job description.
Presentation Layer
Presentation layer generally uses MVC (Model View Controller) pattern. For that it uses Spring MVC. This dependency helps creating application in MVC pattern. Which in turn can be used by client applications to access our website.
Integration Layer
Integration layer is used to communicate with other applications. It uses web services, and now RESTful web services are used more than SOAP. We use Spring Data REST or Spring MVC dependency for this.
Key Class names for Java EE or Spring Boot Applications
We derive all the names from module name. For example in our College Information System, we can have Student module, Teacher module, Subject module. We derive the class name from the module name. For simplicity, we take module name as Student.
1) Modal Class
Represents the data of our domain. Directly mapped to our database model.
____.java
Student.java
Admin.java
2) DAL (Data Access Layer)
Here we have an interface and a class. The implementation and interface has IS-A relationship.
2.1) Interface
I___DAO
IStudentDAO.java
IAdminDAO.java
2.2) Implementation
___DAOImpl
StudentDAOImpl.java
AdminDAOImpl.java
Here,
StudentDAOImpl IS-A IStudentDAO.
StudentDAOImpl à implements à IStudentDAO
3) SL (Service Layer)
Here we have an interface and a class for service layer too. The implementation and interface has IS-A relationship. But it also has relationship with IStudentDAO. It is HAS-A relationship
3.1) Interface
I___Service
IStudentService.java
IAdminService.java
3.2) Implementation
___ServiceImpl
StudentServiceImpl.java
AdminServiceImpl.java
Here,
StudentServiceImpl IS-A IStudentService
StudentServiceImpl àimplements à IStudentService
StudentServiceImpl HAS-A IStudentDAO
StudentServiceImpl àUses à IStudentDAO
We use dependency injection of IStudentDAO into StudentServiceImpl and use it.
4) PL (Presentation Layer)
Last important component in our application is a controller
___Controller
StudentController.java
AdminController.java
Here,
___Controller HAS-A I___Service
StudentController HAS-A IStudentService
List of files
- ____.java
- I___DAO.java
- ___DAOImpl.java
- I___Service.java
- ___ServiceImpl.java
- ___Controller.java
This file structure is used by every Java EE application. But spring provides us some tools that simplifies this for us.
No comments:
Post a Comment