Wednesday, April 28, 2010

Data Abstraction

Concrete Vehicle
public interface Vehicle {
double getFuelTankCapacityInGallons();
double getGallonsOfGasoline();
}

Abstract Vehicle
public interface Vehicle {
double getPercentFuelRemaining();
}

In both of the above cases the second option is preferable. We do not want to expose
the details of our data. Rather we want to express our data in abstract terms. This is not
merely accomplished by using interfaces and/or getters and setters. Serious thought needs
to be put into the best way to represent the data that an object contains. The worst option is
to blithely add getters and setters.



-- Robert Martin / Clean Code

No comments:

Post a Comment