← All categories

LLD Capstones

Small systems to redesign end to end, in the style of machine-coding rounds.

Low-level design capstones give you a working but tangled system, such as a parking lot, an elevator or an expense splitter, and ask you to redesign it. There is no single right pattern; several ideas from the other categories have to work together.

A good approach is to name the things in the system and what changes about each of them: rules that vary become strategies, lifecycles become states, notifications become observers, and the main class goes back to coordinating rather than doing everything.

You'll recognise it when

  • One class with many maps holding parallel pieces of data.
  • Status strings and booleans steering most methods.
  • Pricing, allocation or scheduling rules chosen by flags.
  • New variants of the system require copying the main class.

Example

A coffee kiosk takes orders and calls out names when drinks are ready. Here is how to start a redesign.

Before
class Kiosk {
    Map<Integer, String> drinkByOrder = new HashMap<>();
    Map<Integer, String> statusByOrder = new HashMap<>();   // "NEW", "BREWING", "READY"
    Map<Integer, String> nameByOrder = new HashMap<>();

    void tick() {
        for (int id : statusByOrder.keySet()) {
            if (statusByOrder.get(id).equals("BREWING") && brewed(id)) {
                statusByOrder.put(id, "READY");
                speaker.say(nameByOrder.get(id) + ", your " + drinkByOrder.get(id) + " is ready");
            }
        }
    }
}
After
class Order {
    private final String customer;
    private final Drink drink;
    private OrderStatus status = OrderStatus.NEW;
    void markReady(List<OrderListener> listeners) {
        status = status.moveTo(OrderStatus.READY);
        listeners.forEach(l -> l.ready(this));
    }
}

class Speaker implements OrderListener {
    public void ready(Order o) { say(o.customer() + ", your " + o.drink() + " is ready"); }
}

Why it's better: Data that belongs together lives in one object, status changes go through one checked transition, and new reactions (a screen, an SMS) are new listeners rather than edits to the kiosk loop.

Ideas you'll practise

Finding entities and responsibilitiesStrategies for varying rulesStates for lifecyclesObservers for notificationsTestable, deterministic design

Problems 9