Does the Single Responsibility Principle mean a class should have only one method?
No. It means a class should have one reason to change, serving one actor or concern. A class can have many methods if they all serve that single purpose.
Explain it simply
A cricket scorer keeps runs, wickets and overs. Many actions, but one concern: the score. Adding a "print the ticket" job to the same person would be a second concern.
Count responsibilities by who would ask for changes, not by how many methods exist. If the finance team and the design team both send change requests to the same class, it holds two responsibilities.
Example
class Cart { // one concern: the contents of a cart
void add(Item i) { ... }
void remove(Item i) { ... }
int total() { ... }
int count() { ... }
}
class Cart2 { // mixed concerns
void add(Item i) { ... }
int total() { ... }
void saveToMySql() { ... } // storage
String toInvoiceHtml() { ... } // presentation
}Cart has four methods and still follows SRP. Cart2 has the same number and does not.
What to say in the interview
"SRP is about one reason to change, not one method. I judge it by asking which different people or concerns would force an edit to this class. Many cohesive methods are fine; unrelated ones are the warning sign."
Common mistakes
- Splitting cohesive code into one-method classes just to look tidy.