Skip to the content.

Open/Closed Principle

Open for extension, closed for modification.

The violation

A quote calculator with a switch that must be reopened for every new shipping option:

BigDecimal costFor(String type, BigDecimal total) {
    return switch (type) {
        case "standard" -> ...;
        case "express" -> ...;
        // international launch? edit this method, re-test everything it quotes
        default -> throw new IllegalArgumentException(type);
    };
}

Every new option modifies (and re-risks) tested code, and the calculator accumulates knowledge of every shipping deal the shop has ever offered.

The fix

ShippingQuotes is closed: it quotes whatever ShippingRate implementations it’s given – StandardShipping and ExpressShipping here – and is never edited again. The system is open: launching international delivery means adding one new class and registering it.

Benefits

Relationship to Strategy

The Strategy pattern is the mechanism that makes this possible; OCP is the principle it serves. Most OCP-compliant designs are built from Strategy-shaped extension points.

Example test

ShippingQuotesOcpTest