Metadata Card
- Prerequisites: Chapter 3 (Requirements & Architecture Design)
- Estimated Time: 45 minutes
- Core Difficulty: Intermediate
- Completion Milestone: Design three-tier/four-tier architectures, understand MVC variants
Your Progress
Your first machine: 1500 parts stuffed into one furnace. All gears, springs, levers welded together. Later you partitioned: power section, transmission section, control section.
But over time, the control section mixed in transmission logic, and the transmission section had control wiring. Everything worked, but the repairman couldn't tell "which piece to remove." The City of Artisans standard manual says: layer. Your Task
Large enterprise apps to small personal projects — you need a division of layers. Separate code by responsibility into horizontal tiers, each communicating only with adjacent layers. This is layered architecture, most commonly exemplified by the holy trinity of MVC (Model-View-Controller).
Chapter tiers
- Required reading: Three-tier architecture, MVC
- Selective: MVP, MVVM
- Advanced: Four-tier architecture with domain layer
First Layer: Three-Tier Architecture
Presentation → Business Logic → Data Access. Each layer depends only on the layer directly below.
@RestController // Presentation
@RequestMapping("/players")
public class PlayerController {
private final PlayerService playerService; // Depends on Business
}
@Service // Business Logic
public class PlayerService {
private final PlayerRepository playerRepository;
public void register(String name, int level) {
if (name.length() < 2) throw new IllegalArgumentException();
// Business validation
playerRepository.save(new Player(name, level));
}
}
@Repository // Data Access
public class PlayerRepository {
public void save(Player player) { /* SQL */ }
}Second Layer: Four-Tier Architecture
Interface → Application → Domain → Infrastructure. The domain layer is pure — no framework dependencies.
Third Layer: MVC — Model-View-Controller
Model: data and business logic. View: visual display. Controller: receives user input, coordinates Model and View.
Fourth Layer: MVP — Model-View-Presenter
View is passive — only renders. Presenter handles all logic, making the View testable without a UI framework.
Fifth Layer: MVVM — Model-View-ViewModel
ViewModel is a state projection of the View. View automatically binds to ViewModel changes (React hooks, Vue reactive data).
Common Pitfalls
Layer bypass (Controller calling Repository directly), God Class Service, Presenter too fat, JSON/Map passing between layers instead of DTOs.
Traveler's Notes
Layered architecture is the most straightforward organizational approach. Presentation handles I/O, Business Logic handles rules, Data handles persistence. MVC and its variants further separate UI from logic. Draw the layer diagram before writing code.
Next: Hexagonal & Clean Architecture (Chapter 12).