Search⌘ K

SOLID: Single Responsibility Principle

Explore the Single Responsibility Principle (SRP) to understand how designing classes with only one reason to change improves code simplicity and maintainability. This lesson uses a book invoice example to show how to separate concerns by splitting responsibilities into distinct classes, making future modifications and extensions easier.

Introduction

The Single Responsibility principle (SRP) is perhaps the least understood SOLID concepts. Robert C. Martin coined the term and defines it as follows: “A class should have only one reason to change.” This implies that any class or component in our code should only have one functionality. Everything in the class should be related to just one goal.

When programmers need to add features or new behavior, they frequently integrate everything within the current class. When something needs to be changed later, due to the ...