System Design Academy

Saving…

Lesson 2 of 7 in Module 20 of 45 lessons complete

S — Single Responsibility Principle

Module 2, lesson 2Concept12 min

Definition

Single Responsibility Principle

In plain English
The Single Responsibility Principle says a type should have only one reason to change.
Think of it like
It is like a kitchen where the pastry chef edits recipes only when desserts change, not when prices change.

Reason to change

In plain English
A reason to change is a person, team or rule whose requests would force you to edit the code.
Think of it like
It is like the department that would phone you to ask for an edit.

Why it exists

Finance changes refill pricing. Label printing breaks because both jobs live in the same type.

Two teams also edit the same file. Their edits can conflict, leaving you to combine unrelated changes.

How it works

SOLID

In plain English
SOLID names five design principles: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation and Dependency Inversion.
Think of it like
It is like a five-part checklist for keeping a workshop easy to rearrange.
  1. List change requests

    Name the people or rules that cause edits.

  2. Group related work

    Keep methods together when the same requests change them.

  3. Split unrelated groups

    Give each group a focused type.

  4. Connect the parts

    Use a small coordinating type to call them in order.

RefillService contains three jobs before separation. RefillHandler instead uses Validator, Pricer and LabelPrinter.

Separate refill jobs and connect them through a handler.

RefillService contains three jobs before separation. RefillHandler instead uses Validator, Pricer and LabelPrinter.

One reason to change does not mean one method per type. Several fee methods can belong together when finance owns their rules.

Examples

Follow who requests changes

Pharmacists change validation, finance changes prices, and store operations change label layouts.

Before: validate, price and label ten tablets.
Go
package main

import "fmt"

type RefillService struct{}

func (RefillService) Validate(count int) bool { return count > 0 }
func (RefillService) Price(count int) int     { return count * 20 }
func (RefillService) PrintLabel(count int)    { fmt.Println("tablets", count) }
func main() {
	s := RefillService{}
	if s.Validate(10) {
		fmt.Println(s.Price(10))
		s.PrintLabel(10)
	}
}
After: validate, price and label ten tablets.
Go
package main

import "fmt"

type Validator struct{}

func (Validator) Validate(n int) bool { return n > 0 }

type Pricer struct{}

func (Pricer) Price(n int) int { return n * 20 }

type LabelPrinter struct{}

func (LabelPrinter) PrintLabel(n int) { fmt.Println("tablets", n) }

type RefillHandler struct {
	v      Validator
	p      Pricer
	labels LabelPrinter
}

func (h RefillHandler) Handle(n int) {
	if h.v.Validate(n) {
		fmt.Println(h.p.Price(n))
		h.labels.PrintLabel(n)
	}
}
func main() { RefillHandler{}.Handle(10) }

The insight

Split by who asks for changes.

Trade-offs

Pros

  • Small, focused types.
  • Easier tests.
  • Fewer conflicting edits.

Cons

  • More types to navigate.
  • Too many splits obscure a simple flow.

Use when

  • One type changes for unrelated reasons.
  • Different teams own different rules.

Avoid when

  • Tiny code where everything changes together.
  • A proposed split only separates method names.

Quick check

Which signals an SRP violation?
The Single Responsibility Principle means…

Takeaways

  • One type, one reason to change.
  • Find reasons by asking who requests changes.
  • A small coordinator can wire focused types together.

Try the quick check above whenever you feel ready.

Previous: Coupling & Cohesion