Skip to content
Ameen Abozaid
In reviewJNR-004-L01

Single Responsibility

  • Lesson 1 of 7
  • 10 min read
  • Junior
On This Lesson 4 topics

The content for this lesson is written and waiting for review. The outline below shows what it covers. It will be published once it is approved.

In this lesson

give each class one reason to change.

Planned topics

  • SOLID is five design guidelines for classes. Guidelines, not laws.
  • S: a class should have one reason to change.
  • "One responsibility" does not mean "one method".
  • Smell: a class that changes for tax rules, for email text, and for database changes.
  • Fix: extract classes, each with one clear job.
Draft outline details

Demonstration

java
class OrderService {
    void place(Order o) { /* calculate tax, save, send email */ }
}
// split: TaxCalculator, OrderRepository, OrderNotifier

Check your understanding

Does SRP mean one method per class?

Show answer

No; it means one reason to change.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.