Domain Models

In this blog post, we’ll dive into the concept of domain models, explore the differences and discuss how these choices impact your software design.

WTH is a Domain Model?
Instead of saying something like "the term domain model refers to an abstraction that represents the real-world concepts and behaviors of a system" i find that stackoverflow reply more helpful:

If you were to take your program and strip away the gui and the DBMS, and replace it with a command line interface, and a MySQL server instance, then port the whole thing to a different platform with different system calls and file system access api calls, the part that you didn't change in that process is the domain model.


Types of Domain Models

Rich and Anemic (and Fat).

Anemic
It exposes fields through getters and setters but it does not perform any business logic. All the business logic is handled in other layers often called Application layer or Service Layer.
It achieves cleaner separation of concerns(between data and logic).
Keeps objects smaller and simpler because they only store data.

But the problems
With just getters and setters you loose Encapsulation of OOP.  Services across your application access and change something through setters of your object and this is bad.
As more logic accumulates outside of the domain objects its becomes harder to maintain and evolve system.
And code duplications.



Rich
Ensures object is fully responsible for its own state changes and interactions. Objects carry both data and behavior.
For example BankAccount Object can have logics called:
deposit() withdraw() and transfer()
And service layer becomes thinner and more easy to understand and therefore maintain.

It maybe more complex to achieve than simpler Anemic models.

In general, the more behavior you find in the services, the more likely you are to be robbing yourself of the benefits of a domain model. If all your logic is in services, you've robbed yourself blind.
Martin Fowler


https://martinfowler.com/bliki/AnemicDomainModel.html
https://www.baeldung.com/java-anemic-vs-rich-domain-objects