Skip to workspace
Kotlin Lab

Designing Kotlin APIs / Chapter 04

Decorate a price book

Choose which operations to forward when a wrapper changes the price of an item.

The decision

A promotion subtracts 100 cents from each base price, with a minimum item price of zero. The checkout must use the same rule for a line item and a basket. Currency still belongs to the wrapped price book.

Read the Kotlin

Identify the receiver of the forwarded call

In PriceBook by base, Kotlin supplies forwarding methods for interface members the wrapper leaves alone. A call to the wrapper’s forwarded subtotal runs subtotal on base. Inside that method, a call to unit consequently uses base’s pricing. Overriding unit on the wrapper changes direct wrapper calls. To use the promotion for a basket, its subtotal must invoke unit on the wrapper.

override fun subtotal(items: List<Int>) = items.sumOf(::unit)
// Here ::unit is bound to this Promo instance.

Forward a live property through its getter

An interface val promises a getter. The base can compute a new value each time it is read. Delegating currency lets the base answer each call. Initializing a wrapper field with base.currency instead evaluates the getter once during construction. Change the base currency after creating the wrapper to distinguish those designs.

Apply the discount at the same granularity as the rule

Write a receipt line for each item before adding up the basket. A 100-cent discount floored at zero gives charges of 0 and 200 for items priced at 30 and 300. A single basket discount gives 230. Subtracting twice but flooring only at the end gives 130. Two items, including one cheaper than the discount, separate all three policies.

The full example

Java 25
import java.util.List;

class Lesson {
    interface PriceBook {
        String currency();
        int unit(int cents);
        default int subtotal(List<Integer> items) {
            return items.stream().mapToInt(this::unit).sum();
        }
    }

    record Retail(String currency) implements PriceBook {
        public int unit(int cents) { return cents; }
    }

    record Promo(PriceBook base) implements PriceBook {
        public String currency() { return base.currency(); }
        public int unit(int cents) { return Math.max(0, base.unit(cents) - 100); }
    }
}
Kotlin/JVM
interface PriceBook {
    val currency: String
    fun unit(cents: Int): Int
    fun subtotal(items: List<Int>): Int = items.sumOf(::unit)
}

class Retail(override val currency: String) : PriceBook {
    override fun unit(cents: Int) = cents
}

class Promo(private val base: PriceBook) : PriceBook by base {
    override fun unit(cents: Int) = (base.unit(cents) - 100).coerceAtLeast(0)
    override fun subtotal(items: List<Int>) = items.sumOf(::unit)
}

Inputs and outcomes

InputOutcomeWhy it matters
EUR book, unit price 17575 centsApply the discount to one line.
EUR book, basket [50, 175]75 centsFloor each discounted item before adding: 0 + 75.
CAD book, empty basket0 cents in CADThe promotion preserves the wrapped currency.

Walk through the code

  1. In Java, Promo inherits the interface’s default subtotal method. Its receiver is the Promo instance, so this::unit picks up the promotion for each item.
  2. Kotlin’s PriceBook by base forwards interface members, including subtotal, unless the wrapper overrides them. An unmodified forwarded subtotal runs on base. Calls inside that method use the base price book.
  3. The explicit subtotal override binds ::unit on Promo. Currency can still be forwarded because the promotion has no new rule for it.

The Java habit

Overriding one delegated method does not redirect self-calls made by the delegate. Testing only Promo.unit misses a basket that bypasses the promotion.

When to choose differently

When the wrapper changes almost every operation, forwarding the whole interface conceals the few safe forwards. A smaller price lookup interface can make the dependency clearer.

References

Open quizTry code