Java 3 Tier Architecture





Hey guys in this post, we will discuss the Java 3 tier/layer architecture in detail.

Overview


3-tier architecture is a client-server architecture in which the functional process logic, data access, computer data storage and user interface are developed and maintained as independent modules on separate platforms.

A “tier” in this case can also be referred to as a “layer”. The three tiers, or layers, include:

  • Presentation Layer
  • Service Layer
  • Data Layer

Java-3-layer-architecture-001

Watch the video


In order to understand better, i created a video on Java 3 layer architecture –

Explanation


If you work on any Java enterprise application, that application is divided into 3 main layers that is because if our application starts growing, its very easy to maintain and debug the application. Each layer has its own roles and responsibilities. Let’s discuss these layers one by one in detail.

Presentation layer


Presentation Layer is sometimes called as Controller Layer. The job of the Presentation Layer is to manage the control flow of the application. It manages the HTTP requests.

It accepts the information from the HTTP request and pass that information to the Service Layer for further processing. Once the Service Layer completed its processing, it sends the information back to the Presentation Layer. Its the responsibility of the Presentation Layer to prepare the information that are required by the client and send it back to the client as a HTTP response.

The Presentation Layer contains Model classes, nothing but POJOs (Plain Old Java Objects) which contains private fields, setters, getters and toString().

Presentation Layer never communicate with data layer directly. It sends only the information to the Service Layer for further processing. That’s the job of the Presentation Layer.

Service layer


Next is the Service Layer, which is the middle layer, it sits between the Presentation Layer and Data Layer.

The job of the Service Layer is to perform the business logic. It performs most of the heavy lifting work of our application. It contains the complex business logic. The Service Layer, sometimes call other services outside of the world for further processing, it might even call other REST services. The Service Layer, even communicate with Data Layer for further processing such as reading the data, writing the data and other database operations.




The Service Layer, contains the DTO (Data Transfer Objects) objects, these are nothing but POJOs, which contains private fields, setters, getters and toString(). These are the objects that travel back and forth from Presentation Layer to Service Layer and Service Layer to Data Layer and vice versa. The Service Layer accepts the information in the form DTO objects and return the information in the form of DTO objects. These conversions will happen with the help of Object Mapper or Bean Utils.

Please note that Service Layer never accepts HTTP requests and it never manages the control flow of the application.

Data Layer


The last layer is the Data Layer. It responsible for communicating with the database. It performs all the database operations such as create, read, update and delete, it even contains database queries which are required for the application.

The Data Layer contains, DAO (Data Access Objects) objects or sometimes called as Repositories, these are the one which contains all the database things, like creating queries, deleting queries or some other sql queries.

The Data Layer also contains Entity objects, these are the POJOs which contains, private fields, setters, getters and toString(). These are the objects that will persist in the database. These objects also contains some annotations (with Spring and JPA) which are required to persist the data, establish relationship between the other entities.

Please note that, Presentation Layer objects never access the Data Layer objects similarly Data Layer objects never return as HTTP response because these objects contains some sensitive information which we should not expose to the outside world. Also, the Data Layer never accepts HTTP requests neither it never manages the control flow of the application.

Alright, that’s it. That’s all about Java 3 Layer Architecture. If you found this article useful, please share this with your friend and colleague.



Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply