Layered
Layered Architecture organizes a software system into horizontal layers, where each layer has a specific responsibility and interacts only with the layers directly above or below it.
The main goal is to:
- Improve maintainability
- Increase modularity
- Support separation of concerns
- Make systems easier to test and modify
Typical Layers in Object-Oriented Architectureβ
Although the number of layers can vary, a common structure includes:
Presentation Layer (UI Layer)β
- Handles user interaction.
- Displays data and collects user input.
- Contains UI classes, controllers, or views.
Examples: Web pages, mobile screens, API controllers.
Application Layer (Service / Business Layer)β
- Coordinates application tasks.
- Contains business rules and workflows.
- Acts as a bridge between UI and data layers.
Examples: OrderService, PaymentProcessor, UserManager.
Domain Layer (Business Logic / Model Layer)β
- Represents core business concepts and rules.
- Contains domain objects (entities, value objects).
Examples: Order, Customer, Invoice, Product.
Data Access Layer (Persistence Layer)β
Handles storage and retrieval of data.
Isolates the database from the rest of the system.
Examples: Repositories, DAOs, database mappers.
Infrastructure Layer (Optional)β
- Provides technical services such as logging, email, caching, file systems, or external APIs.
Rules of Layered Architectureβ
- Each layer has a defined responsibility.
- Upper layers depend on lower layers, not the other way around.
- A layer should only communicate with the layer directly below it (strict layering).
- Layers should be replaceable without affecting others (e.g., switching databases).
Example: Online Shopping Systemβ
Letβs model an Online Shopping System using layered architecture.
Presentation Layerβ
class OrderController {
private OrderService orderService;
public void placeOrder(int customerId, List<Item> items) {
orderService.processOrder(customerId, items);
}
}
Role: Receives user requests and sends them to the service layer.
Application / Service Layerβ
class OrderService {
private OrderRepository orderRepository;
private PaymentService paymentService;
public void processOrder(int customerId, List<Item> items) {
Order order = new Order(customerId, items);
paymentService.processPayment(order);
orderRepository.save(order);
}
}
Role: Implements business workflows like order processing and payment.
Domain Layerβ
class Order {
private int customerId;
private List<Item> items;
public Order(int customerId, List<Item> items) {
this.customerId = customerId;
this.items = items;
}
public double calculateTotal() {
return items.stream().mapToDouble(Item::getPrice).sum();
}
}
Role: Represents core business objects and rules.
Data Access Layerβ
class OrderRepository {
public void save(Order order) {
// Save order to database
}
public Order findById(int id) {
// Retrieve order from database
return null;
}
}
Role: Manages persistence and database interactions.
How the Layers Work Togetherβ
User β UI β Service β Domain β Data β Database
Example flow:
- User places an order through the UI.
- Presentation layer sends request to service layer.
- Service layer creates domain objects and applies business rules.
- Data layer stores the order.
- Result flows back up to the user.
Advantages of Layered Architecture
- Clear separation of concerns
- Easier maintenance and updates
- Improved testability
- Supports parallel development
- Encourages reuse
Disadvantages
- Can introduce performance overhead
- May become rigid for complex workflows
- Changes in lower layers can still affect upper layers