CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 3 of 6 · 1 min read

The problem

A class that picks its own helper.

Here is code that chooses a notifier by a text code.

Notifier n;
if (kind.equals("EMAIL")) n = new Email();
else if (kind.equals("SMS")) n = new Sms();
else throw new IllegalArgumentException("unknown kind: " + kind);
n.send("hi");

This works, but the choice sits in the middle of business code. Every class that needs a notifier would repeat these lines. When a push channel arrives, every copy must change. This is the switch smell of Level 2, module 2, and the duplication of module 7.

HF meets the same problem in a pizza store. The orderPizza method decides which pizza class to create, and it also prepares, bakes and boxes the pizza. The creation part changes every time the menu changes, and the rest stays still. HF's advice is to pull the changing part out and put it in a separate object.

Check yourself: 1. What breaks when a push channel is added? 2. Which part of HF's orderPizza changes often?