BolEnglishLearn · Play · Win
Login

🎯 Software Design Principles · Core Principles

🎯

Single Responsibility Principle

Design PrincipleCoreModule 2

A component should have one clear reason to change.

Do not make one class responsible for customers, payments, PDFs, and emails.

💡 In practice

OrderService manages order rules while EmailService sends emails.

🖼️ CONCEPT PICTURE
A picture to help remember Single Responsibility Principle
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.

💡 Learn🛠️ Apply🎯 Check

💡 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

1
Name the job
Try to describe the class in one sentence without using the word "and". If you need "and", it is doing too much.
2
Split by reason to change
Group code by who would ask for a change. Billing rules and email formatting change for different reasons, so keep them apart.
3
Give each piece its own home
Move each responsibility into its own class or file with a clear name that says what it does.
4
Keep the glue thin
Let a small coordinator call each piece. Do not let the coordinator grow into a giant that does the work itself.

⚠️ 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.
⚠️ Common mistake: Bundling unrelated jobs into one "do everything" class because it felt convenient. Split each responsibility into its own focused class with a clear name.

🎯 Quick check

Score: 0/3

Three questions to test the idea.

  1. 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. 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. 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.

🔑 Key terms

Responsibility
One job or reason a piece of code exists.
Reason to change
A kind of future request that would force you to edit this code.
Cohesion
How well the parts of one unit belong together.
God class
A single class that tries to do far too much.

✅ Review checklist

📊 Your progress

0%
Mark this topic learned to track it.
Saved in this browser.
🚀 Got it?
Mark it learned and move to the next topic.
Next topic →