System Design Academy

Saving…

Lesson 4 of 7 in Module 20 of 45 lessons complete

L — Liskov Substitution Principle

Module 2, lesson 4Concept12 min

Definition

Liskov Substitution Principle

In plain English
The Liskov Substitution Principle says any implementation of an interface must work wherever that interface is expected, without surprising the caller.
Think of it like
It is like any brand of AA battery working in any AA device.

Contract

In plain English
A contract is the set of promises an interface makes about what it accepts and what it gives back, including errors.
Think of it like
It is like a rental agreement listing what both sides promise.

Why it exists

Your reminder code receives no error, so it records delivery. One notifier silently dropped the message.

Matching method names are not enough. Callers also rely on accepted inputs, required steps and error behaviour.

How it works

Go has no class inheritance. Its interfaces still need implementations that keep their promises.

  • Do not require hidden setup steps from the caller.
  • Do not silently skip promised work.
  • Return errors in the form the contract describes.
  • Do not panic because you chose not to support a promised method.

DiskStorage satisfies Storage. ReadOnlyArchive claims Storage but panics on Save; Loader fits the archive, while DiskStorage can also satisfy Saver.

A read-only archive needs a smaller promise.

DiskStorage satisfies Storage. ReadOnlyArchive claims Storage but panics on Save; Loader fits the archive, while DiskStorage can also satisfy Saver.

Split reading into Loader and writing into Saver. A read-only archive can then promise reading without pretending to support writing.

Examples

Report a dropped reminder

Notifier promises an error when a message is not delivered. PagerNotifier silently drops messages longer than 160 characters.

Before: try to deliver a 161-character reminder; printing represents successful delivery.
Go
package main

import (
	"fmt"
	"strings"
)

type Notifier interface{ Send(string) error }
type PagerNotifier struct{}

func (PagerNotifier) Send(message string) error {
	if len([]rune(message)) > 160 {
		return nil
	}
	fmt.Println("delivered", message)
	return nil
}
func main() {
	var n Notifier = PagerNotifier{}
	fmt.Println(n.Send(strings.Repeat("a", 161)))
}
After: try to deliver a 161-character reminder; printing represents successful delivery.
Go
package main

import (
	"fmt"
	"strings"
)

type Notifier interface{ Send(string) error }
type PagerNotifier struct{}

func (PagerNotifier) Send(message string) error {
	if len([]rune(message)) > 160 {
		return fmt.Errorf("message exceeds 160 characters")
	}
	fmt.Println("delivered", message)
	return nil
}
func main() {
	var n Notifier = PagerNotifier{}
	fmt.Println(n.Send(strings.Repeat("a", 161)))
}

The insight

Silently doing less breaks the delivery promise.

Trade-offs

Pros

  • Different implementations remain safe to substitute.
  • Callers need fewer special cases.

Cons

  • Promises must be written down.
  • Some interfaces must be split.

Use when

  • An interface has multiple implementations.
  • Callers depend on specific errors or setup steps.

Avoid when

  • A type can only fulfil half the promise.
  • Use a smaller interface instead of unsupported methods.

Quick check

A ReadOnlyArchive satisfies Storage, but its Save method panics. What's wrong?
A new store returns a generic error instead of ErrNotFound for missing keys. Why is that a Liskov problem?

Takeaways

  • Every implementation must keep the interface's promises.
  • Errors and required steps are part of the promise.
  • If a type can't keep the whole promise, give it a smaller interface.

Try the quick check above whenever you feel ready.

Previous: O — Open/Closed Principle