Day 5 - Objects and data structures
Data abstraction
-
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
public interface Vehicle { double getFuelTankCapactiyInGallons(); double getGallonsOfGasoline(); }
->
public interface Vehicle { int getPercentFuelRemaining(); }
Data/object anti-symmetry
- Objects hide their data behind abstractions and expose functions that operate on that data. Data structure expose their data and have no meaningful functions
The law of demeter
- wiki
-
A module should not know about the innards of the objects it manipulates. An object should not expose its internal structure though accessors because to do so is to expose, rather than to hide, its internal structure
final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath();
->
Options opts = ctxt.getOptions; File scratchDir = opts.getScratchDir(); final String outputDir = scratchDir.getAbsolutePath();
Anyway, is correct to write:
final String outputDir = ctxt.options.scratchDir.absolutePath;
Data transfer object
- wiki
- A class with public variables and no functions. Is very useful when communicating (especially with databases)