What are the hyperaemic model and the anaemic model?
Introduction
"What are the rich model and the anaemic model?"It seems basic, but in reality, it conceals many details. The following discussion will proceed in a manner that prioritises conclusions.
Core Analysis
Domain-Driven Design is an object-oriented software design approach that emphasises encapsulating business logic within domain objects.
Suppose there is an e-commerce website that needs to perform operations such as purchasing products and managing inventory. In the rich domain model, you can define a Product class to represent a product. The Product class will contain the attributes of the product (such as name, price, stock, etc.), and will also contain some behaviours (such as purchasing the product, updating the stock, etc.). These behaviours are directly encapsulated in the Product class to facilitate operations on the products.
public class Product{
private String productName;
private BigDecimal price;
private Long stock;
public void purchase(int quantity) {
if (quantity > stock) {
throw new IllegalArgumentException("Not enough stock available");
}
stock -= quantity;
}
}
The Anemic Domain Model is a model that separates data from behaviour, where data is held by objects and behaviour is provided by external services.
In an anaemia model, the Product class may only contain the attributes of the product (such as name, price, stock, etc.), while the behaviours are provided by other classes or services. For example, we might create a ProductService class to provide operations such as purchasing products and updating inventory, while the Product class is only used to store data. In this way, the Product class is merely a passive data holder and does not have its own behaviour.
public class Product {
private String name;
private double price;
private int stock;
public Product(String name, double price, int stock) {
this.name = name;
this.price = price;
this.stock = stock;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getStock() {
return stock;
}
}
public class ProductService {
public void purchase(Product product, int quantity) {
if (quantity > product.getStock()) {
throw new IllegalArgumentException("Not enough stock available");
}
product.setStock(product.getStock() - quantity);
}
}
Further reading
Advantages, disadvantages and applicable scenarios
Advantages of the Anemic Model:
- Object-oriented design, with good encapsulation and maintainability.
- Domain objects contain their own business logic, making them easy to understand and extend.
- It can avoid over-reliance on external services and improve the stability of the system.
Disadvantages of the Anemic Model:
- An understanding of the model is required for better development, and the learning curve is steep.
- Collaboration between objects may increase, leading to a more complex design.
- The state of objects may become inconsistent, requiring special attention.
Advantages of the anaemic model:
- Separation of data and behaviour reduces the complexity of objects.
- It can improve the reusability and testability of code.
- Existing services and frameworks can be better utilised.
Disadvantages of the anaemic model:
- Objects lack encapsulation, making them prone to coupling and fragility.
- The business logic is scattered across multiple classes, making it difficult to maintain and understand.
- Excessive reliance on external services may lead to system instability.
Generally, the anaemic model can be used for smaller application systems or simple business processes, while the rich model is recommended for larger application systems or complex business processes.
Summary of Key Points
- The Rich Domain Model (Domain-Driven Design) is an object-oriented software design approach that emphasises encapsulating business logic within domain objects.
- In the rich model, you can define a Product class to represent a product.
-
- It can avoid over-reliance on external services and improve the stability of the system.
- Disadvantages of the Anemic Model: 1. An understanding of the model is required for better development, and the learning curve is steep. 2. Collaboration between objects may increase, leading to a more complex design.
由站内小编编辑整理