Chapter 3. Anemic Models
Correctness is clearly the prime quality. If a system does not do what it is supposed to do, then everything else about it matters little.
Bertrand Meyer, Object-Oriented Software Construction
3.0 Introduction
Anemic domain models, or simply anemic objects, are objects consisting of a bunch of attributes without real behavior. An anemic object is often referred to as a “data object” because it mainly serves to store data, but it lacks any meaningful methods or operations that can be performed on that data.
Exposing data to the outside world through getters and setters can violate the principle of encapsulation, as it allows external sources to access and potentially modify the data within an object rather than keeping it contained within the object itself. This can make the object more susceptible to corruption or unintended changes.
The anemic way of designing can also lead to a more procedural style of programming, where the main focus is on manipulating data rather than encapsulating it within objects that have meaningful behavior. This book encourages you to create rich objects. Rich objects have a more robust set of behaviors and methods that allow them to perform meaningful operations and provide a single point of access, avoiding repeated logic.
Encapsulation
Encapsulation refers to protecting the responsibilities of an object. You can usually achieve this by abstracting the actual implementation. It also provides a way to control access to ...
Get Clean Code Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.