System Design Academy

Saving…

Lesson 5 of 7 in Module 20 of 45 lessons complete

I — Interface Segregation Principle

Module 2, lesson 5Concept12 min

Definition

Interface Segregation Principle

In plain English
The Interface Segregation Principle says no code should be forced to depend on methods it does not use.
Think of it like
It is like a child's TV remote with only the few buttons they need instead of all sixty.

Why it exists

A door lock must provide SetTemp to satisfy Device. There is no honest temperature-setting behaviour for that lock.

Your report needs one play count. Its test replacement still needs ten methods because the accepted interface is too large.

How it works

Fat interface

In plain English
A fat interface has many methods, most of which any one caller doesn't need.
Think of it like
Like a Swiss Army knife when you only wanted a bottle opener.
Before: one library interface mixes seven device operations.
Go
package devices

type Device interface {
	TurnOn()
	TurnOff()
	SetTemp(int)
	SetBrightness(int)
	Lock()
	Unlock()
	Battery() int
}

Interface embedding

In plain English
Interface embedding builds a bigger Go interface by listing smaller interfaces inside it.
Think of it like
Like clipping a charger and a cable together into a charging kit.
After: small library interfaces describe separate capabilities.
Go
package devices

type Switcher interface {
	TurnOn()
	TurnOff()
}
type Thermostat interface{ SetTemp(int) }
type Lock interface {
	Lock()
	Unlock()
}
type Dimmer interface{ SetBrightness(int) }
type BatteryReader interface{ Battery() int }
type SmartLight interface {
	Switcher
	Dimmer
}

SmartLight embeds Switcher and Dimmer, and Light satisfies it. DoorLock satisfies Lock; RoomThermostat satisfies Thermostat.

Devices promise only their own capabilities.

SmartLight embeds Switcher and Dimmer, and Light satisfies it. DoorLock satisfies Lock; RoomThermostat satisfies Thermostat.

  • io.Reader describes reading bytes; io.Writer describes writing bytes.
  • io.ReadWriter embeds both interfaces for callers that need both.
  • Define a small interface beside the code that uses it.

Examples

Give a door lock an honest interface

The home hub mixes seven device operations. Its door lock has to fake temperature changes.

Before: describe the operations needed to secure a door.
Go
package devices

type Device interface {
	TurnOn()
	TurnOff()
	SetTemp(int)
	SetBrightness(int)
	Lock()
	Unlock()
	Battery() int
}
type DoorLock struct{}

func (DoorLock) TurnOn()           {}
func (DoorLock) TurnOff()          {}
func (DoorLock) SetTemp(int)       {}
func (DoorLock) SetBrightness(int) {}
func (DoorLock) Lock()             {}
func (DoorLock) Unlock()           {}
func (DoorLock) Battery() int      { return 90 }

var _ Device = DoorLock{}
After: describe the operations needed to secure a door.
Go
package main

import "fmt"

type Lock interface {
	Lock()
	Unlock()
}
type DoorLock struct{}

func (DoorLock) Lock()   { fmt.Println("locked") }
func (DoorLock) Unlock() { fmt.Println("unlocked") }
func Secure(lock Lock)   { lock.Lock() }
func main()              { Secure(DoorLock{}) }

The insight

Implementers should only promise what they can do.

Trade-offs

Pros

  • Small test replacements.
  • Fewer changes spread to unrelated implementers.
  • Each interface states a focused need.

Cons

  • More interfaces to name.
  • Related interfaces can be harder to find.

Use when

  • Callers need only part of an interface.
  • Implementers cannot support every promised method.

Avoid when

  • Every caller uses the whole interface.
  • A split adds names without separating needs.

Quick check

A door lock must implement SetTemp() to satisfy the hub's Device interface. Which principle is violated?
In Go, how do you build a ReadWriter from Reader and Writer?

Takeaways

  • Don't make code depend on methods it doesn't use.
  • Prefer several small interfaces over one fat one.
  • In Go, define interfaces where they're used and combine them by embedding.

Try the quick check above whenever you feel ready.

Previous: L — Liskov Substitution Principle