Objects and Data Structures
We do not want to expose the details of our data. Rather we want to express our data in abstract terms.
Hiding implementation is not just a matter of putting a layer of functions between the variables. Hiding implementation is about abstractions! A class does not simply push its variables out through getters and setters. Rather it exposes abstract interfaces that allow its users to manipulate the essence of the data, without having to know its implementation.
Data structures expose data and have no significant behavior. This makes it easy to add new functions to existing data structures but makes it hard to add new data structures to existing functions because all the functions must change.
Objects expose behavior and hide data. This makes it easy to add new kinds of objects without changing existing behaviors. It also makes it hard to add new behaviors to existing objects because all the classes must change.
In any given system, we will sometimes want the flexibility to add new data types, and so we prefer objects for that part of the system. Other times, we will want the flexibility to add new behaviors, and so in that part of the system, we prefer data types and procedures.
Example of Data structure:
Example of Object:
Hybrid structures are half object and half data structures.
Hybrids make it hard to add new functions but also make it hard to add new data structures. They are the worst of both worlds.
The quintessential form of a data structure is a class with public variables and no functions. This is sometimes called a data transfer object, or DTO.
DTO is an object that carries data between processes. DTO is a simple object that should not contain any business logic but may contain serialization and deserialization mechanisms for transferring data over the wire.