System Design Academy

Saving…

Lesson 4 of 10 in Module 10 of 45 lessons complete

Abstraction with Interfaces

Module 1, lesson 4Concept12 min

Definition

Abstraction

In plain English
Abstraction means exposing what something does while hiding how it does it.
Think of it like
It is like a car's pedals, where you press "go" without knowing how the engine works.

Interface

In plain English
A Go interface is a named set of method signatures that any type satisfies by having those methods.
Think of it like
It is like a job description, where anyone who can do the listed tasks qualifies.

Why it exists

Your clinic switches SMS providers. If reminder code names the old provider everywhere, you must edit each caller and retest each path.

Ask for the operation you need: notify this recipient. Reminder code stays the same when you switch delivery providers.

How it works

Implicit satisfaction

In plain English
Implicit satisfaction means a Go type fits an interface automatically by having its required methods.
Think of it like
It is like qualifying as a licensed driver by holding a licence with no extra sign-up.
SendReminder needs an operation, not a particular delivery type.
Go
package main

import "fmt"

type Notifier interface{ Notify(to, msg string) error }
type SMS struct{}

func (SMS) Notify(to, msg string) error { fmt.Println("SMS", to, msg); return nil }

type Email struct{}

func (Email) Notify(to, msg string) error      { fmt.Println("email", to, msg); return nil }
func SendReminder(n Notifier, to string) error { return n.Notify(to, "Appointment tomorrow") }
func main() {
	if err := SendReminder(SMS{}, "+10000000000"); err != nil {
		panic(err)
	}
	if err := SendReminder(Email{}, "you@example.test"); err != nil {
		panic(err)
	}
}

Notifier requires Notify with recipient and message strings, returning an error. SMS and Email satisfy Notifier, and Reminders uses that interface.

SMS and Email satisfy the contract used by reminders.

Notifier requires Notify with recipient and message strings, returning an error. SMS and Email satisfy Notifier, and Reminders uses that interface.

  • Keep interfaces small, often one or two methods.
  • Define an interface in the package that uses it.
  • Accept interfaces and return structs when callers need replaceable behaviour.
  • T has value-receiver methods. *T has both receiver kinds. Use *T when the interface needs a pointer-receiver method.

Abstract class

In plain English
An abstract class is a half-built class in languages like Java that cannot be used on its own, and Go has no such thing.
Think of it like
It is like a half-finished template that must be completed before use.

Fake

In plain English
A fake is a simple stand-in implementation used in tests instead of the real thing.
Think of it like
It is like a crash-test dummy standing in for a real driver.

Examples

Appointment reminders

You send the same appointment reminder through SMS or email. Later, a push delivery type can provide the same Notify method.

SendReminder needs an operation, not a particular delivery type.
Go
package main

import "fmt"

type Notifier interface{ Notify(to, msg string) error }
type SMS struct{}

func (SMS) Notify(to, msg string) error { fmt.Println("SMS", to, msg); return nil }

type Email struct{}

func (Email) Notify(to, msg string) error      { fmt.Println("email", to, msg); return nil }
func SendReminder(n Notifier, to string) error { return n.Notify(to, "Appointment tomorrow") }
func main() {
	if err := SendReminder(SMS{}, "+10000000000"); err != nil {
		panic(err)
	}
	if err := SendReminder(Email{}, "you@example.test"); err != nil {
		panic(err)
	}
}

Define Notifier beside the reminder code that uses it. Providers only need matching methods; they do not import the reminder package.

The insight

Callers depend on what, not how.

Trade-offs

Pros

  • Swap implementations without changing callers.
  • Test with inexpensive fakes.
  • Reduce knowledge of implementation details.

Cons

  • Following calls through interfaces takes extra reading.
  • Interfaces without a purpose add clutter.

Use when

  • The same caller needs two implementations.
  • Tests need a fake.

Avoid when

  • One implementation with no need for a test replacement.
  • The concrete type already expresses the task well.

Quick check

type Sensor interface{ Read() (Reading, error) }. A new BrandX struct has Read() (Reading, error). What must you add so BrandX satisfies Sensor?
Where does Go idiom suggest defining an interface?

Takeaways

  • Abstraction = what, not how.
  • Matching methods satisfy Go interfaces without an implements declaration.
  • Keep interfaces small and define them where they're used.

Try the quick check above whenever you feel ready.

Previous: Encapsulation