What do coupling and cohesion mean in software design, and what do good values look like in Java?
Coupling is how much one class depends on another. Cohesion is how much the parts of one class belong to the same job. You want low coupling and high cohesion.
public class Main {
public static void main(String[] args) {
Report r = new Report(new PlainFormatter());
r.addLine("alpha");
System.out.println(r.render());
}
}
And here is the class with low coupling and high cohesion.
public class Report {
private final Formatter formatter;
private final java.util.List<String> lines = new java.util.ArrayList<>();
public Report(Formatter formatter) {
this.formatter = formatter;
}
public void addLine(String line) {
lines.add(line);
}
public String render() {
StringBuilder out = new StringBuilder();
for (String line : lines) {
out.append(formatter.format(line)).append('\n');
}
return out.toString();
}
}
So what does coupling mean exactly? It is how much the code of one class depends on the code of another. And there are two kinds of it that you should know about.
the kind what it means
-------------------- --------------------------------------------
loose coupling the class depends on a small, simple
interface
tight coupling the class depends on the details of another
class, such as its fields or its internals
And the difference between them is what you can change. A loosely coupled class keeps working when the other one changes its insides. A tightly coupled one breaks.
// Tightly coupled. The report reaches into the list.
public class Report {
private final java.util.List<String> lines = new java.util.ArrayList<>();
public void render() {
for (int i = 0; i < lines.size(); i++) {
System.out.println(lines.get(i));
}
}
}
// Loosely coupled. The report only asks for what it needs.
public class Report {
private final Formatter formatter;
public Report(Formatter formatter) {
this.formatter = formatter;
}
public String render() {
return formatter.format(lines);
}
}
And cohesion is about the inside of one class. A class with high cohesion has all its parts working on the same job. A class with low cohesion is a collection of things that happen to be in the same file.
the sign of low cohesion what it should be
------------------------------------ -------------------------------
a class that reads a file, sends three classes
an email and does maths
a method that is 200 lines long several methods
fields that half of them are used smaller classes
by half of the methods
And there is one more thing that is worth knowing about, and it is about the signs of low coupling.
the sign of tight coupling what it means
------------------------------------ ------------------------------
a method takes a concrete class it cannot be given a fake
a field is declared as a concrete the class cannot be tested
class
the code reaches into the fields it knows the insides
of another object
So the rule to remember is this. Low coupling means the class depends on small interfaces, and high cohesion means the class does one job.
The next two questions in this topic are about spotting them in a review and about why they are worth the work.