Everyone can recite it: "a class should have only one reason to change." Almost nobody can apply it under pressure. The gap is that SRP isn't about class size. It's about who asks for changes.
The test: count the stakeholders
A "responsibility" is a source of change requests. If the pricing team, the compliance team, and the platform team can all force edits to the same class, that class has three responsibilities, even if it's forty lines long.
The class interviewers love to hand you
Here's the classic bait. It looks tidy. It compiles. It ships.
public class InvoiceManager {
public Money calculateTotal(Invoice invoice) {
// tax rules, discounts, rounding policy
}
public void saveToDatabase(Invoice invoice) { /* JDBC everywhere */ }
public String renderPdf(Invoice invoice) { /* fonts, layout, logos */ }
}
Ask the stakeholder question: tax rules change (finance), storage moves to a new schema (platform), the PDF gets a rebrand (design). Three teams, three reasons, one file. Every change risks the other two behaviors, and the unit tests now need a database and a PDF renderer just to verify a tax rule.
The fix is unglamorous and that's the point:
public class InvoiceCalculator { // changes when business rules change
public Money calculateTotal(Invoice invoice) { /* ... */ }
}
public class InvoiceRepository { // changes when storage changes
public void save(Invoice invoice) { /* ... */ }
}
public class InvoicePdfRenderer { // changes when presentation changes
public String render(Invoice invoice) { /* ... */ }
}How this shows up in your interview
You'll rarely be asked "explain SRP." Instead, it's embedded in the follow-ups of every
design question. In the parking lot: should ParkingLot compute fees? No: fees change
with business whims (weekend pricing, EV discounts), while the lot's structure doesn't.
That instinct, "pricing will change on its own schedule, so it gets its own class",
is SRP, spoken like an engineer instead of a textbook.
✎ Check yourself
A `ReportGenerator` fetches data, computes stats, and emails the result. The email provider is being replaced next quarter. What does SRP suggest?