Understanding the Single Responsibility Principle

Andrei Birta
3 min readFeb 1, 2023

--

The Single Responsibility Principle is a crucial programming concept that dictates that each module or class in a web application should be responsible for a single aspect of the software’s functionality and that this responsibility should be fully contained within the class. All services provided by the class should be directly related to this.

To fully understand the Single Responsibility Principle, consider that a class or module should only need to be modified for a single reason. When a class has multiple responsibilities, it may need to be altered for multiple reasons, which can make it more challenging to comprehend and keep up-to-date.

An illustration of the Single Responsibility Principle in action could be a class designed to handle reading and writing data to a file. This class might include methods for opening and closing the file, reading and writing data, and potentially formatting the data in a specific way. All of these methods pertain to the class’s responsibility of reading and writing data to a file, and any modifications to the class would probably be related to this responsibility.

Conversely, if the same class were also responsible for displaying data on the screen, it would have an extra responsibility. This might lead to the class needing to be changed for two different reasons: to incorporate new features related to reading and writing data, or to add new features related to displaying data on the screen. This can make the class more challenging to comprehend and maintain.

Using the Single Responsibility Principle allows for the design of a software system that is more modular, with each class or module having a specific, well-defined responsibility. This can make the system easier to understand, maintain, and expand, as changes to one part of the system are less likely to affect other parts.

Here is an example of the Single Responsibility Principle in PHP:

class FileHandler {
public function readFile($filePath) {
// read data from file
}

public function writeFile($filePath, $data) {
// write data to file
}
}

class DataFormatter {
public function formatData($data) {
// format data
}
}

class DataProcessor {
private $fileHandler;
private $formatter;

public function __construct(FileHandler $fileHandler, DataFormatter $formatter) {
$this->fileHandler = $fileHandler;
$this->formatter = $formatter;
}

public function processData($filePath) {
$data = $this->fileHandler->readFile($filePath);
$formattedData = $this->formatter->formatData($data);
$this->fileHandler->writeFile($filePath, $formattedData);
}
}

In this example, the FileHandler class is responsible for reading and writing data to a file, while the DataFormatter class is responsible for formatting data. The DataProcessor class has a single responsibility: to process data by reading it from a file, formatting it and writing it back to the file. This is achieved by using the FileHandler and DataFormatter classes to handle the specific tasks of reading and writing data and formatting data, respectively. By following the Single Responsibility Principle, the classes in this example are more modular and easier to maintain.

Get more valuable insights and tips by following me!
Also check my list of articles about the basics in PHP or advanced.

--

--