System Design Academy

Saving…

Lesson 6 of 10 in Module 10 of 45 lessons complete

Polymorphism via Interfaces

Module 1, lesson 6Concept12 min

Definition

Polymorphism

In plain English
Polymorphism means one piece of code can work with many different types through a shared interface.
Think of it like
It is like a universal power socket that accepts any appliance with the right plug.

Dynamic dispatch

In plain English
Dynamic dispatch means the program decides while running which type's method to call.
Think of it like
It is like a receptionist forwarding "speak to support" to whichever agent is on shift.

Why it exists

Airport staff add a transfer-bag category. You must update the routing switch and every other function that repeats the bag-type list.

Before: a central switch names every bag kind.
Go
package main

import "fmt"

type Bag struct{ Kind string }
type Belt string

func Route(bag Bag) Belt {
	switch bag.Kind {
	case "standard":
		return "main"
	case "oversize":
		return "wide"
	case "transfer":
		return "connections"
	default:
		return "manual"
	}
}
func main() { fmt.Println(Route(Bag{Kind: "oversize"})) }

Give each bag type its own routing method. The sorter asks every bag the same question instead of checking its type.

How it works

After: one sorter loop calls each bag’s routing method.
Go
package main

import "fmt"

type Belt string
type Routable interface{ Route() Belt }
type StandardBag struct{}

func (StandardBag) Route() Belt { return "main" }

type OversizeBag struct{}

func (OversizeBag) Route() Belt { return "wide" }

type TransferBag struct{}

func (TransferBag) Route() Belt { return "connections" }
func main() {
	bags := []Routable{StandardBag{}, OversizeBag{}, TransferBag{}}
	for _, bag := range bags {
		fmt.Println(bag.Route())
	}
}

Routable requires a Route method returning a Belt. StandardBag, OversizeBag and TransferBag satisfy it; Sorter uses the Routable interface.

The sorter depends on routing behaviour.

Routable requires a Route method returning a Belt. StandardBag, OversizeBag and TransferBag satisfy it; Sorter uses the Routable interface.

  1. Store a concrete value

    An interface value stores a value together with its actual type.

  2. Call the interface method

    The program selects the method belonging to that stored type.

  3. Run the implementation

    The bag's method chooses the belt. The sorter does not check the bag type.

Type switch

In plain English
A type switch lets Go code check which concrete type is stored in an interface value.
Think of it like
It is like opening a mystery box to see what is inside before deciding what to do.
Go fragment: a type switch inspects the stored concrete type.
Go
func Label(x Routable) string {
	switch x.(type) {
	case OversizeBag:
		return "wide handling"
	default:
		return "ordinary handling"
	}
}

Examples

Add a bag without editing the sorter

The revised design gives StandardBag, OversizeBag and TransferBag their own routing methods.

After: one sorter loop calls each bag’s routing method.
Go
package main

import "fmt"

type Belt string
type Routable interface{ Route() Belt }
type StandardBag struct{}

func (StandardBag) Route() Belt { return "main" }

type OversizeBag struct{}

func (OversizeBag) Route() Belt { return "wide" }

type TransferBag struct{}

func (TransferBag) Route() Belt { return "connections" }
func main() {
	bags := []Routable{StandardBag{}, OversizeBag{}, TransferBag{}}
	for _, bag := range bags {
		fmt.Println(bag.Route())
	}
}

Pass the sorter a new type with a Route method. Its loop stays the same.

The insight

New types plug in without editing the sorter.

Trade-offs

Pros

  • Add types without editing callers.
  • Remove repeated switch chains.

Cons

  • Behaviour is spread across implementations.
  • Seeing every case can require opening several files.

Use when

  • The set of types grows.
  • Several places repeat the same type checks.

Avoid when

  • A tiny fixed set fits one readable switch.
  • Types have no meaningful shared operation.

Quick check

You add a GroupPass to the ski gate. With the interface design, what changes in the gate loop?
You find the same switch v := x.(type) in six places. What does it most likely signal?

Takeaways

  • Polymorphism = one call, many types.
  • Go does it through interfaces.
  • Repeated type switches hint at a missing method.

Try the quick check above whenever you feel ready.

Previous: Inheritance — and Why Go Uses Embedding Instead