SOLID: Single Responsibility Principle
Get familiar with the Single Responsibility principle along with its examples.
We'll cover the following...
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 complexity of the code, the update process becomes extremely time-consuming and tedious. The Single Responsibility Principle helps us create simple classes that perform just one task. This makes making modifications or adding extensions to the existing code much easier.
Real-life example
The following illustration represents how SRP is applied in real life: