🎯 Software Design Principles · Core Principles
Single Responsibility Principle
A component should have one clear reason to change.
Do not make one class responsible for customers, payments, PDFs, and emails.
OrderService manages order rules while EmailService sends emails.
💡 Think of a Swiss Army knife that is also your phone, your wallet and your car key: when one part breaks, everything stops working.
💡 Why it matters
When one class does many unrelated jobs, a small change to one job can quietly break the others. Code that each has a single job is easier to read, test and change safely. It also lets different people work on different parts without stepping on each other.
🛠️ How to achieve it
⚠️ Signs of trouble
- •A single class is hundreds of lines long and keeps growing.
- •You cannot describe the class without listing many unrelated jobs.
- •A change to one feature forces edits all over the same file.
- •The class name is vague, like Manager, Helper or Util.
🎯 Quick check
Score: 0/3Three questions to test the idea.
1. What does "one reason to change" mean for a class?
Answer: Only one kind of future request should force you to edit it
A single reason to change means only one sort of requirement touches that class, so edits stay small and safe.
2. Which class name is the warning sign of too many responsibilities?
Answer: A vague name like Manager or Util
Vague names like Manager or Util usually hide a grab bag of unrelated jobs.
3. Order rules and sending emails are mixed in one class. What is the fix?
Answer: Split them into OrderService and EmailService
They change for different reasons, so each belongs in its own focused class.
