August 15, 2026 β€’ Java

00 - Types of Layered Architectures

Layered Architecture

Client
   β”‚
   β–Ό
Controller
   β”‚
   β–Ό
Service
   β”‚
   β–Ό
Repository
   β”‚
   β–Ό
Database

Responsibilities

  • Controller β†’ Receives HTTP requests and returns HTTP responses.
  • Service β†’ Contains the application’s business logic.
  • Repository β†’ Handles communication with the database.
  • Entity β†’ Represents a database table (JPA).
  • DTO (Data Transfer Object) β†’ Object used for API requests and responses.
  • Config β†’ Spring configuration classes.
  • Utility β†’ Reusable helper functions.

Feature-based Architecture (Package by Feature)

user/
 β”œβ”€β”€ UserController
 β”œβ”€β”€ UserService
 β”œβ”€β”€ UserRepository
 β”œβ”€β”€ UserDTO
 └── User

product/
 β”œβ”€β”€ ProductController
 β”œβ”€β”€ ProductService
 β”œβ”€β”€ ProductRepository
 β”œβ”€β”€ ProductDTO
 └── Product

Each feature contains all the classes related to a specific business capability.


Hexagonal Architecture (Ports & Adapters)

Commonly used in large enterprise applications, fintechs, and banks.

Controller
      β”‚
      β–Ό
Application Service
      β”‚
      β–Ό
Domain
      β–²
      β”‚
Repository Interface
      β–²
      β”‚
JPA Repository

The domain layer does not depend on Spring, HTTP, or the database.

Advantages

  • Easy to test
  • Loosely coupled
  • Easy to replace the database
  • Easy to replace REST with Kafka, GraphQL, or gRPC

Clean Architecture

An evolution of Hexagonal Architecture.

Controller
      β”‚
      β–Ό
Use Cases
      β”‚
      β–Ό
Domain
      β”‚
      β–Ό
Infrastructure
← Back to blog