binary, code, privacy policy-1327493.jpg

Single Responsibility Principle (SRP)

The SOLID Single Responsibility Principle is a software design principle that states that a class or module should have one and only one reason to change. In other words, it should have a single, well-defined responsibility or function. This principle intents to make software designs more understandable, flexible, and maintainable by promoting loose coupling and high cohesion.

The idea behind the SRP is that if a class or module has multiple responsibilities, changes to one responsibility may inadvertently affect the other responsibilities, making the code harder to understand and maintain. Isolating each responsibility in a separate class or module, makes code easier to understand, test and modify.

In practice, this means that a class should have a single, well-defined purpose, and its methods should all contribute to fulfilling that purpose. If a class has multiple responsibilities, split it into multiple classes, each with its own specific responsibility.

SRP example in Python

Here is an example of a class that does not adhere to the Single Responsibility Principle:


					class FileHandler:
                        def __init__(self, file_name):
                            self.file_name = file_name
                        def read_file(self):
                          with open(self.file_name, 'r') as f:
                              return f.read()
                        def write_file(self, data):
                           with open(self.file_name, 'w') as f:
                               f.write(data)
                        def encrypt_file(self):
                            data = self.read_file()
                            encrypted_data = encrypt(data)
                            self.write_file(encrypted_data)
				

In this example, the FileHandler class is responsible for reading and writing to a file and encrypting the file’s contents. This violates the Single Responsibility Principle because the class has multiple responsibilities. If we want to change the encryption algorithm, for example, when want to modify the class, even though the change is not related to reading or writing to the file.

Here’s an example of how we can refactor the above class to adhere to the Single Responsibility Principle:


					class FileReader:
                        def __init__(self, file_name):
                            self.file_name = file_name
                        def read_file(self):
                            with open(self.file_name, 'r') as f:
                              return f.read()
                    class FileWriter:
                        def __init__(self, file_name):
                            self.file_name = file_name
                        def write_file(self, data):
                            with open(self.file_name, 'w') as f:
                                f.write(data)
                    class FileEncryptor:
                        def encrypt_file(self, data):
                          return encrypt(data)

				

Now we have three classes, each with a single responsibility. FileReader is responsible for reading the file, FileWriter
is responsible for writing to the file, and FileEncryptor is responsible for encrypting the file. If we want to change the
encryption algorithm, we only need to modify the FileEncryptor class, and it won’t affect the FileReader
and FileWriter classes.

SRP is the first of the five SOLID principles that support you to design and development easy maintainable and extendible software.