A parent pays the fee for an engineering entrance (JEE) coaching batch. A minute later, the parent gets a receipt on WhatsApp with the course fee, the scholarship, the goods and services tax (GST) and the total. Behind that message, the institute's code does three jobs: it calculates the money, it writes the receipt, and it sends it.
The Single Responsibility Principle says that a class should have one reason to change. In this article we see what a "reason" is, build the fee receipt twice, and look at the opposite mistake, splitting a class too far.
The principle
A reason to change is a person or a team who asks for changes. The accountant asks for changes to how the fee and the GST are calculated. The centre head asks for changes to what the receipt says. Marketing asks for changes to how the receipt reaches parents. These three people ask for different changes, for different reasons, and they do not plan their changes together.
So the principle is about grouping code by who changes it. Code that the same person asks to change belongs together. Code that different people ask to change belongs in different classes, so that one person's request does not edit another person's class.
The principle does not say "one method per class", and it does not ask for tiny classes. A fee calculator can have several methods, for the fee, a refund and a late fee, and still have one reason to change, because the accountant asks for all of them. Size is not the test. The test is how many different people would ask you to edit the class.
Example: a coaching institute's fee receipt
An Enrolment holds the student, the batch, the course fee and a scholarship percentage. The first version does everything in one method:
public class FeeReceiptService { private static final int GST_PERCENT = 18; private final WhatsAppClient whatsApp; public FeeReceiptService(WhatsAppClient whatsApp) { this.whatsApp = whatsApp; } public void issue(Enrolment enrolment) { long fee = enrolment.courseFee(); long gst = fee * GST_PERCENT / 100; long scholarship = fee * enrolment.scholarshipPercent() / 100; long total = fee + gst - scholarship; String receipt = "Fee receipt: " + enrolment.student().name() + ", " + enrolment.batch() + "\nCourse fee " + dollars(fee) + "\nScholarship -" + dollars(scholarship) + "\nGST " + dollars(gst) + "\nTotal paid " + dollars(total); whatsApp.send(enrolment.student().phone(), receipt); } private static String dollars(long amount) { return String.format("$%,d", amount); }}This code works, and it is short. Then two requests arrive in the same month, from two different people.
The accountant says GST must be calculated on the fee after the scholarship. For a $5,000 course with a 10% scholarship, the code charges GST on $5,000, which is $900. It should charge GST on $4,500, which is $810. The receipt says $5,400 where it should say $5,310.
Marketing says many parents do not read WhatsApp receipts, so the receipt must go by email too.
Both changes edit issue(), and that has three costs:
- The accountant's rule cannot be tested alone.
issue()returns nothing. A test of the GST rule must build a fake WhatsApp client, callissue(), and read the numbers back out of the message text. - Marketing's change puts the money at risk. The email change adds a second client to the constructor and a new line to
issue(), a few lines below the GST calculation. A reviewer has to check that the fee calculation did not move. - Nobody else can reuse the calculation. The admissions page wants to show the fee before a student joins. The only way to get the fee is to call
issue(), and that sends a WhatsApp message.
Splitting along the reasons
We give each person their own class. The accountant's rules go into FeeCalculator, which returns a FeeBreakup record:
public record FeeBreakup(long courseFee, long scholarship, long gst) { public long total() { return courseFee - scholarship + gst; }} public class FeeCalculator { private static final int GST_PERCENT = 18; public FeeBreakup calculate(Enrolment enrolment) { long fee = enrolment.courseFee(); long scholarship = fee * enrolment.scholarshipPercent() / 100; long gst = (fee - scholarship) * GST_PERCENT / 100; return new FeeBreakup(fee, scholarship, gst); }}The accountant's change is now the gst line, and it touches no other class. A test calls calculate and checks three numbers. The admissions page calls the same method, and nothing is sent.
Notice what stays together. The GST rate, the scholarship and the order they apply in are all the accountant's rules, so they sit in one class. Splitting them apart would mean one request from the accountant edits several classes.
The centre head's part goes into ReceiptFormatter. Its format(enrolment, fees) method holds every line of the receipt text and the dollars helper. Marketing's part becomes an interface with one class per channel:
public interface ReceiptChannel { void send(Student student, String receipt);} public class EmailChannel implements ReceiptChannel { private final EmailClient email; public EmailChannel(EmailClient email) { this.email = email; } @Override public void send(Student student, String receipt) { if (student.email() != null) { email.send(student.email(), "Your fee receipt", receipt); } }}WhatsAppChannel has the same shape and calls the WhatsApp client. A student without an email address still gets the WhatsApp receipt, because EmailChannel skips that student and the other channels still run.
The three parts still have to run in order for every payment. FeeReceiptDesk does that and nothing else:
import java.util.List; public class FeeReceiptDesk { private final FeeCalculator calculator; private final ReceiptFormatter formatter; private final List<ReceiptChannel> channels; public FeeReceiptDesk(FeeCalculator calculator, ReceiptFormatter formatter, List<ReceiptChannel> channels) { this.calculator = calculator; this.formatter = formatter; this.channels = List.copyOf(channels); } public void issue(Enrolment enrolment) { FeeBreakup fees = calculator.calculate(enrolment); String receipt = formatter.format(enrolment, fees); for (ReceiptChannel channel : channels) { channel.send(enrolment.student(), receipt); } }}The desk holds no fee rule, no receipt text and no client. It changes only if the steps themselves change, for example if a receipt must be saved before it is sent.
The email change is now a new class and one line where the desk is built, List.of(new WhatsAppChannel(whatsApp), new EmailChannel(email)). FeeCalculator and the GST line did not change.
Splitting too far
The same principle tells us when to stop. Suppose someone splits the fee calculation into tiny classes: GstRate, ScholarshipRule, GstCalculator, FeeTotal, and a FeeCalculator that calls them. Each class has one method, and each looks like it follows the principle. GstCalculator.gstFor(enrolment) reads the course fee from the enrolment itself.
Then the accountant's request arrives again. FeeCalculator must work out the scholarship first, and GstCalculator must now be given the scholarship too. One rule, from one person, now edits two classes, and the two edits must ship together.
These five classes have one reason to change between them, so they are one responsibility. Splitting them did not separate two people's code. It only spread one person's code across more files.
When not to
A small script that one person writes, runs and changes does not need this split. If the institute has one centre, one owner and a receipt that has not changed in a year, FeeReceiptService is fine. Split a class when a second person starts asking for changes to it, or when one part has to be used without the others, like the admissions page needing the fee. Before that, the split is five extra files for a change that may never come.
In an interview
When you draw a class, say who would ask you to change it. If the answer is two different people, split it there and say why. The Parking Lot design keeps the fee in a PricingStrategy outside ParkingLot, so a new tariff never edits the lot. The Facade page shows the same "one class runs the steps" shape as FeeReceiptDesk.
Common mistakes:
- Counting methods. A class with one public method can still have three reasons to change, like
FeeReceiptServiceabove. - Splitting by step, not by person.
GstRateandGstCalculatorare two steps with the same owner. - A coordinator that holds rules. If
FeeReceiptDeskstarted adding the GST itself, the accountant's changes would land in two classes again. - Splitting before the need exists. An interface for one channel, "in case", is extra code to read.
Summary
- A class should have one reason to change. A reason is a person or a team who asks for changes.
- Group code by who changes it, not by how small the class is.
- Split along those reasons, and keep each person's rules together in one class.
- One class that runs the steps in order is fine, as long as it holds no one's rules.
- If one request edits several classes, the split went too far.