Skip to main content

Single Responsibility

The Single Responsibility Principle (SRP) is one of the five SOLID principles of object-oriented design, introduced by Robert C. Martin. SRP advocates that a class should have only one reason to change, meaning it should have only one responsibility or encapsulate a single functionality.

Key principles of the Single Responsibility Principle (SRP):

  1. One Responsibility:

    • A class should encapsulate one and only one responsibility or functionality.
    • This principle aims to promote a clear and concise design where each class has a focused purpose.
  2. Reason to Change:

    • The "reason to change" refers to the motivation behind modifying a class.
    • If a class has multiple responsibilities and one aspect of the system changes, it may necessitate modifications to that class, potentially affecting other unrelated responsibilities.
  3. Enhanced Maintainability:

    • By adhering to SRP, code becomes more maintainable as each class focuses on a specific task.
    • Changes related to a particular functionality are isolated to the class responsible for that functionality, reducing the likelihood of unintended side effects.
  4. Improved Readability and Understanding:

    • Code is easier to read and understand when each class has a clear and singular purpose.
    • SRP encourages developers to create smaller, focused classes, making the codebase more comprehensible.
  5. Flexible Design:

    • SRP facilitates a more flexible and adaptable design.
    • When a system evolves or requirements change, classes adhering to SRP are less likely to be affected by modifications, as their responsibilities are well-defined and isolated.

Example:

Consider a class that manages both file input/output and user authentication. Applying SRP, these responsibilities should be separated into distinct classes. One class could handle file operations, and another could be responsible for user authentication. This separation adheres to the principle, ensuring that changes in one aspect of the system don't impact the other.

# Without SRP
class FileManagerAndAuthenticator:
def read_file(self, filename):
# Read file implementation

def write_file(self, filename, content):
# Write file implementation

def authenticate_user(self, username, password):
# User authentication implementation

# With SRP
class FileManager:
def read_file(self, filename):
# Read file implementation

def write_file(self, filename, content):
# Write file implementation

class Authenticator:
def authenticate_user(self, username, password):
# User authentication implementation

By adhering to SRP, the responsibilities of file management and user authentication are clearly separated, leading to a more maintainable and modular design.