liveBook · Manning

Cohesive class

As explained in Software Design for Python Programmers

A cohesive class is a class whose methods and instance variables collectively serve one closely related primary responsibility.

A cohesive class gives its methods and instance variables one closely related primary responsibility. Its purpose remains clear, and maintenance stays localized because the class has only one reason to change. section 2.3.1 section 4.2.1

Separate automobile responsibilities

A single Automobile class that accelerates, adjusts headlights, changes oil, changes tires, and cleans the car mixes operations, maintenance, and cleaning. Refactor it into Automobile for automobile operations, Garage for maintenance, and CarWash for cleaning; Garage and CarWash receive the Automobile they operate on. section 4.2.1

Book catalogue

Book stores title and author attributes, while Catalogue maintains Book objects, adds entries, and finds matches. Case-insensitive matching treats an empty target attribute as a don't-care, allowing searches such as every book by King. section 2.3.1

How Software Design for Python Programmers covers it

How deep it goes
Defines cohesion and demonstrates responsibility separation through Book/Catalogue and Automobile refactorings, without descending into formal metrics.
Examples
Uses Book and Catalogue classes, case-insensitive catalogue searches, and Automobile, Garage, and CarWash refactorings.
What it assumes
Assumes basic classes, methods, instance variables, and constructors while introducing the Single Responsibility Principle through concrete Python examples.
What you can do after
Evaluate class boundaries and refactor mixed responsibilities into smaller classes with one primary responsibility.
How it connects
Connects cohesion with the Single Responsibility Principle, coupling, maintenance, and refactoring.

Also explained in

Compare how 2 books explain cohesive class →

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.