What does the single responsibility principle mean in practice, and how do you split a class that breaks it?
It means that a class should have one reason to change. So one job, one set of reasons why the business would ask for a change, and one group of data that belongs to that job.
public class Main {
public static void main(String[] args) {
Order order = new Order();
order.add("Book", 20.0);
System.out.println(order.total());
}
}
And here is a class that breaks it. It has four different reasons to change.
public class Order {
private final java.util.List<String> lines = new java.util.ArrayList<>();
private double total;
private String email;
private java.util.Map<String, String> saved = new java.util.HashMap<>();
public void add(String name, double price) {
lines.add(name);
total = total + price;
}
// Reason one: the business rules change.
public double total() {
return total;
}
// Reason two: the email template changes.
public String confirmationText() {
return "Thanks for your order of " + total;
}
// Reason three: the storage changes.
public void save() {
saved.put(email, confirmationText());
}
// Reason four: the printing changes.
public void print() {
System.out.println(confirmationText());
}
}
And how do you split it? You group the methods by the reason they change, and you move the data with them.
// The order. It only knows about the lines and the total.
public class Order {
private final java.util.List<Line> lines = new java.util.ArrayList<>();
private final Discount discount;
public Order(Discount discount) {
this.discount = discount;
}
public void add(Line line) {
lines.add(line);
}
public double total() {
double sum = 0;
for (Line line : lines) {
sum = sum + line.price();
}
return discount.applyTo(sum);
}
public java.util.List<Line> lines() {
return java.util.Collections.unmodifiableList(lines);
}
}
// The text. It only knows about the wording.
public class ConfirmationText {
private final String template;
public ConfirmationText(String template) {
this.template = template;
}
public String forOrder(Order order) {
return template + " " + order.total();
}
}
// The storage. It only knows about saving.
public class OrderStore {
private final java.util.Map<String, String> saved = new java.util.HashMap<>();
public void save(String id, String text) {
saved.put(id, text);
}
}
And now the reasons to change are separate. A change to the wording changes the ConfirmationText. A change to where the orders are saved changes the OrderStore. A change to the pricing changes the Discount. And a change to what an order is changes the Order.
the class the reason to change
-------------------- --------------------------------------------
Order what an order is
ConfirmationText what the email says
OrderStore where the orders are kept
And there is one benefit that is worth knowing about, and it is about testing. The Order can be tested with no email and no storage at all, so the test is short and it fails for one reason at a time.
So the rule to remember is this. One class, one reason to change, and the data that belongs to that reason.
The last question in this topic is about how to decide whether a class has one responsibility.