How does information hiding reduce the cost of change, and what happens to callers when an implementation detail changes?
Information hiding reduces the cost of change because the number of places that have to change when the inside changes goes down. The fewer callers can see the detail, the fewer callers you have to edit.
public class Main {
public static void main(String[] args) {
Report r = new Report();
r.addLine("alpha");
r.addLine("beta");
System.out.println(r.render());
}
}
And here is the class, and you can see what is hidden.
public class Report {
private final java.util.List<String> lines = new java.util.ArrayList<>();
private final java.util.List<String> header = new java.util.ArrayList<>();
public void addLine(String line) {
lines.add(line);
}
public String render() {
StringBuilder out = new StringBuilder();
for (String line : header) {
out.append(line).append('\n');
}
for (String line : lines) {
out.append(line).append('\n');
}
return out.toString();
}
}
So what happens to the callers when an implementation detail changes? Nothing at all. Here is the proof.
// The detail changes from a list to an array.
public class Report {
private String[] lines = new String[16];
private int count = 0;
public void addLine(String line) {
if (count == lines.length) {
String[] bigger = new String[lines.length * 2];
System.arraycopy(lines, 0, bigger, 0, lines.length);
lines = bigger;
}
lines[count] = line;
count = count + 1;
}
public String render() {
StringBuilder out = new StringBuilder();
for (int i = 0; i < count; i++) {
out.append(lines[i]).append('\n');
}
return out.toString();
}
}
The caller still calls addLine and render and it does not know that the inside changed. That is the whole of the benefit.
the change the number of files to edit
-------------------------- --------------------------------------------
the list becomes an array 1, just the Report class
the list becomes a queue 1, just the Report class
the format of the output 1, if the format was hidden
changes
And there is a second benefit that is worth knowing about, and it is bigger than the first one. The hidden detail can be changed without anyone knowing. So the design can improve over time, and nobody outside has to agree to it.
And there is a third benefit, and it is about the bugs. When the detail is hidden then there is only one place that can get it wrong. So when the bug is found there is one fix and it fixes it for everybody.
the detail is hidden the detail is exposed
-------------------------- --------------------------------------------
one place to get it wrong many places to get it wrong
one fix a fix everywhere
the design can improve the design is frozen by the callers
And there is one more thing that is worth knowing about, and it is about the word "information". It is not only about fields. Any decision that a caller could depend on is information, and it should be hidden too. So the choice of a data structure, the order that things come back in, and the name of a constant are all information, and all of them should be behind the abstraction.
So the rule to remember is this. The benefit is that the number of places to edit when the inside changes goes down to one.
The last question in this topic is about the costs and when to expose a detail.