System Design Academy

Saving…

Lesson 3 of 7 in Module 20 of 45 lessons complete

O — Open/Closed Principle

Module 2, lesson 3Concept12 min

Definition

Open/Closed Principle

In plain English
The Open/Closed Principle says code should be open to new behaviour but closed to editing what already works.
Think of it like
It is like a power strip, where you plug in new devices without rewiring the strip.

Why it exists

You add a pricing plan to a long switch. An edit to a shared branch changes an old plan's price.

A stable caller can use different types through one interface. You add a type for each new behaviour.

How it works

Extension point

In plain English
An extension point is a deliberate place, usually an interface, where new behaviour can plug in.
Think of it like
Like an empty socket on that power strip.
  1. Find recurring variation

    Look for a switch that grows with each feature.

  2. Name the shared operation

    Define the interface the caller needs.

  3. Separate the cases

    Move each behaviour into its own type.

  4. Keep the caller stable

    Choose and connect the new type outside the caller.

FlatRate, TimeOfDay and Membership satisfy PricingRule. ChargingSession depends on that interface.

New pricing types satisfy the same interface.

FlatRate, TimeOfDay and Membership satisfy PricingRule. ChargingSession depends on that interface.

Examples

Add a solar-surplus price

Session code switches between flat and peak prices. A solar-surplus plan would add another branch.

Before: price a four-unit charging session.
Go
package main

import "fmt"

func Charge(plan string, units int) int {
	switch plan {
	case "flat":
		return units * 20
	case "peak":
		return units * 30
	default:
		return 0
	}
}
func main() { fmt.Println(Charge("flat", 4)) }
After: price a four-unit charging session.
Go
package main

import "fmt"

type PricingRule interface{ Price(units int) int }
type FlatRate struct{}

func (FlatRate) Price(units int) int { return units * 20 }

type TimeOfDay struct{}

func (TimeOfDay) Price(units int) int { return units * 30 }

type SolarSurplus struct{}

func (SolarSurplus) Price(units int) int     { return units * 10 }
func Charge(rule PricingRule, units int) int { return rule.Price(units) }
func main() {
	fmt.Println(Charge(FlatRate{}, 4))
	fmt.Println(Charge(SolarSurplus{}, 4))
}

The insight

New behaviour arrives as a new type while Charge stays unchanged.

Trade-offs

Pros

  • Add variants without editing the shared caller.
  • Reduce accidental breaks in existing cases.

Cons

  • More steps between a call and its implementation.
  • Unused extension points add complexity.

Use when

  • A set of variants keeps growing.
  • Several behaviours share one useful operation.

Avoid when

  • Variants are few and fixed.
  • No real variation has appeared.

Quick check

Adding a new pricing plan means editing a 200-line switch inside the charging-session code. Which principle is being violated?
In Go, what usually provides the "open" part?

Takeaways

  • Add behaviour by adding code, not by editing working code.
  • Interfaces, rule lists and registries are Go's extension points.
  • Only build them where change really happens.

Try the quick check above whenever you feel ready.

Previous: S — Single Responsibility Principle