Junior — Middle
Can you explain the concept of the anemic model and what it is about?
sobes.tech AI
Answer from AI
Anemic Domain Model — is an anti-pattern in object-oriented design where objects contain only data (fields) without business logic, and all logic is implemented outside these objects, for example, in services or controllers.
The essence of the anemic model is that domain objects become 'flat' data structures, which contradicts the principles of object-oriented design, where data and behavior should be encapsulated together. This can lead to less maintainable and less extensible code.
Example of an anemic model:
class User {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
}
// Age verification logic is separated
func isAdult(user: User) -> Bool {
return user.age >= 18
}
In contrast, in a rich domain model, the isAdult method would be part of the User class.