AlgoViz

Single responsibility (SRP)

medium 50 min

A class should have one reason to change: it answers to one group of people.

Watch lesson4:20

Where it shows up

The same pattern appears across everyday software.

  • Postfix

    The mail server is a group of small programs. One receives, one cleans up, one manages the queue, one delivers. Its overview page gives each program one part of the way of a message.

  • Kubernetes controllers

    The documentation says that it is useful to have simple controllers rather than one, monolithic set of control loops that are interlinked. Each tracks at least one kind of resource.

  • Docker images

    The best practices say that each container should have only one concern, so that a web application, its database and its cache run in separate containers.

  • React components

    Thinking in React advises that a component should ideally be concerned with one thing, and be cut into smaller ones when it grows.

  • Python logging

    Loggers are what the program calls, handlers send records somewhere, filters choose, formatters decide the layout. Four classes for four wishes.

  • Spring MVC

    The DispatcherServlet gives the work to special beans: one maps requests to handlers, one calls them, one resolves errors, one resolves views. A coordinator with replaceable parts.

  • Git plumbing and porcelain

    Low-level commands each do one small job and can be chained. The commands that people type are built on top of them.

  • PMD GodClass rule

    A static check for Java that looks for classes which do too many things. It combines three metrics, among them how many foreign data a class uses and how cohesive it is.

  • Active Record, the other choice

    In this pattern one object wraps a row, knows the database and adds domain logic. Fowler describes it as fitting for logic that is not too complex: a known trade of separation for speed.

  • Everyday domains

    Shop: order, payment, notification. Bank: transfer, fraud check, audit. Social app: post, notification, analytics. Clinic: patients, appointments, records. Game: player, score, sound. Streaming: player, recommendations, buffer. In each the pieces have different owners, and that is the reason for the cut.