A system's architecture can be seen as a set of structures that give reason to the system. These structures comprise software elements, the relationships between them, and their properties.
Layered architecture is a technique for organizing a system's code into layers, where each layer uses the services of the one below it.
We can see it like a cake, where each layer rests on top of the previous one.
# Common Layers
The commonly used form of layered architecture is with 3 layers.
-
Presentation Layer: This is the ground floor of the building, the facade. It's what users see and interact with. Here, the user interface, data presentation, and user interactions are handled. It's like the lounge where guests are received and shown where everything is.
-
Domain or Business Logic Layer: Going up to the first floor, we have the business logic. This is where the specific business rules and processes are, what gives meaning to the system, the fundamental part. It's like the kitchen, where it's decided what's going to be cooked and how it will be served.
-
Data Source Layer: In the basement, we have the data access layer. It's the layer that interacts with databases or any other data storage. It's like the pantry where all the food and drinks are stored before being prepared and served.
-
Service Layer: Sometimes there's an additional floor, the service layer, which acts as an intermediary between presentation and business logic. It's like having a team of waiters who take the food from the kitchen to the lounge.
# Advantages
-
verified
Separation of Concerns
Each layer has a clear responsibility, which facilitates management and maintenance.
-
verified
Flexibility and Scalability
A layer can be modified or scaled without affecting the others.
-
verified
Ease of Testing
Independent layers allow for more focused and effective testing.
# Disadvantages
-
warning
Performance
Sometimes, passing data between layers can affect performance.
-
warning
Complexity
For small systems, this model can be excessively complex.
# Example: Order Management
We must develop a system to manage orders in a restaurant. The system should allow customers to place food orders and have these orders processed by the kitchen staff.
Proposed Solution
We propose a 3-layer solution with the following package structure:
presentation/: Classes related to user interaction, such asUserInterface.domain/: Classes representing business logic:OrderandChef.data/: Handles data access and manipulation, represented by theWarehouseclass.main/: Contains theRestaurantclass, which coordinates the program's start.
# Java Code
Presentation: UserInterface.java
public class UserInterface {
private Chef chef;
public UserInterface(Chef chef) {
this.chef = chef;
}
public void placeOrder(String item) {
Order order = new Order(item);
System.out.println(order.describeOrder());
String result = chef.prepareFood(order);
System.out.println(result);
}
}
Domain: Order.java
public class Order {
private String item;
public Order(String item) {
this.item = item;
}
public String getItem() {
return item;
}
public String describeOrder() {
return "Order received for: " + item;
}
}
Domain: Chef.java
public class Chef {
private Warehouse warehouse;
public Chef(Warehouse warehouse) {
this.warehouse = warehouse;
}
public String prepareFood(Order order) {
String ingredients = warehouse.getIngredients(order.getItem());
return "Food prepared with " + ingredients + " for " + order.getItem();
}
}
Data: Warehouse.java
public class Warehouse {
public String getIngredients(String item) {
// Simulating ingredient retrieval
return "tomato, cheese, dough";
}
}
Main: Restaurant.java
public class Restaurant {
public static void main(String[] args) {
Warehouse warehouse = new Warehouse();
Chef chef = new Chef(warehouse);
UserInterface ui = new UserInterface(chef);
ui.placeOrder("pizza");
}
}
# Conclusions
In summary, layered software architecture is like a well-organized building or a well-planned party, where each element has its place and purpose, working together to create a harmonious and functional experience.
Summary
Layered architecture allows for a clear separation of concerns, facilitating the change and evolution of individual parts of the system without affecting the rest, as long as the boundaries of each layer are respected.