Diseño Orientado a Objetos
OOD
Escrito por Darjus Kazakevic · revisado el
Keep data and the rules that protect it in the same place.
Object-oriented design (OOD) puts data and the rules about that data behind one boundary: the object. A basket keeps its lines private. The only way to change them is a method that checks the rules. So callers cannot create an invalid basket, even by mistake. This is the default style in Java, C# and most enterprise TypeScript.
The cost is indirection. Behavior lives with the data, so to follow a change you follow a graph of objects. With polymorphism (one call, many implementations), the code that runs depends on the object's type at runtime. OOD works best where rules about state matter more than raw speed.
En esencia — Bundle each piece of state with the methods that guard it, so code outside the object cannot reach an invalid state.
Cuándo recurrir a él
Recurre a él cuando
- State has rules that callers must never be able to skip
- Behavior varies by kind, and new kinds keep arriving
- A long-lived domain model that many developers change
Reconsidéralo cuando
- A hot loop over millions of records, where memory layout matters most
- A one-off script with no rules worth protecting
- Pure data transformation, such as an ETL step or a report
Conceptos clave
- Encapsulation
- An object hides its fields and exposes only methods. Every change goes through code that can check the rules, so each rule is written once, in one place.
- Polymorphism
- Several classes implement one interface, and the caller does not care which one it holds. A new kind of behavior becomes a new class, not a new branch in every switch.
- Composition over inheritance
- Build an object by giving it other objects, not by extending a base class. Deep inheritance trees tie every subclass to the details of its parents.
- SOLID
- Five principles for class design from Robert C. Martin: single responsibility, open/closed, Liskov substitution, interface segregation and dependency inversion. They work best as review questions, not as laws.
Reglas de diseño
- Make invalid states unreachable
- Keep state private and change it only through methods that check the rules. If callers can set fields directly, every caller must remember the rule, and one of them will forget.
- Replace the type switch with a class
- When behavior depends on a kind, give each kind its own class behind one interface. A new discount is then a new class, not a new case in every switch statement.
- Tell objects what to do; don't ask for data
- Call basket.add() instead of reading basket.lines and changing them outside. Code that reads an object's fields to make a decision for it belongs inside that object.
Errores habituales
- A getter and setter for every field. The object guards nothing, and the rules spread into service classes.
- Deep inheritance trees. A change to a base class breaks a subclass three levels down.
- A “God object” such as OrderManager that does everything. Every change touches it, and every test needs it.
- Interfaces with one implementation, added “just in case”. They add a file and a jump, and protect nothing.