liveBook · Manning

Single responsibility principle

As explained in Software Design for Python Programmers

The Single Responsibility Principle states that a class should have one primary responsibility and remain cohesive around it.

The Single Responsibility Principle keeps each class cohesive around one primary responsibility. A clear responsibility makes a class easier to use and maintain because its methods and instance variables serve one purpose. section 2.3.1

A cohesive book catalogue

A Book stores a book’s title and author attributes. A Catalogue maintains Book objects, adds new books, and finds matches. The separation keeps book data distinct from collection management and searching. section 2.3.1

Apply the principle by checking whether a class has a single, recognizable purpose:

  • Use later design principles to manage change, delegation, coupling, extension, interfaces, and duplication without creating unnecessary complexity. section 2.4

How Software Design for Python Programmers covers it

How deep it goes
Defines cohesion and demonstrates responsibility allocation, while deferring broader design tradeoffs to iterative development.
Examples
Uses Python Book and Catalogue classes, catalogue searches, wildcard-like empty attributes, and concrete search output.
What it assumes
Assumes basic Python classes, methods, and instance variables while illustrating responsibility separation through a small application.
What you can do after
Evaluate class designs by identifying whether their data and methods serve one clear primary purpose.
How it connects
Connects cohesion with Encapsulate What Varies, Delegation, Least Knowledge, Open-Closed, Code to the Interface, and Don't Repeat Yourself.

Also explained in

Compare how 7 books explain single responsibility principle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.