Part 7 of 7 · 2 min read
Exercises
Easy
Easy 1: Match problems (20 minutes)
Match these to a pattern: "undo in an editor", "an order that moves through stages", "a price rule that varies by customer", "a legacy class with the wrong method names".
Done when: four problems each have a pattern and a one-line reason.
Hint
Ask what varies in each.
Easy 2: Run the notification system (20 minutes)
Type the notification code with a VendorSms class and an Events class. Publish one event.
Done when: you print the vendor output and the log line from section 4.
Hint
Events holds a list of Consumer<String>.
Easy 3: Intent sentences (20 minutes)
Write one intent sentence for each of Strategy, State, Decorator and Adapter, in your own words.
Done when: four sentences exist, and each says who chooses or what changes.
Hint
Strategy is chosen from outside, State changes inside.
Medium
Medium 1: Look-alike pairs (45 minutes)
For each of three pairs in section 3, write a short code example where the wrong choice would hurt.
Done when: three examples each show the problem caused by the wrong pattern.
Hint
Use wrong code first, then fix it.
Medium 2: Pick for a parking lot (45 minutes)
List the parts of a parking lot design (fee rules, ticket life cycle, display board, payment gateway). Assign a pattern to each, or "none" with a reason.
Done when: four parts each have a decision and at least one says "none".
Hint
A plain Display class may need no pattern.
Medium 3: Swap the channel (45 minutes)
Add an email channel to the notification system. List every class edited and every class added.
Done when: the booking code is unedited, and only the factory changes.
Hint
A new adapter and a factory branch.
Hard
Hard 1: Review a design (90 minutes)
Take a design of your own with five or more patterns. For each, say what problem it solves and what it costs. Remove any that do not earn their place.
Done when: a before and after diagram shows at least one pattern removed, with a reason.
Hint
Count classes added by each pattern.
Hard 2: Read a library (90 minutes)
Pick the Java standard library and find one example of each: Strategy, Observer, Decorator, Adapter and Iterator. Name the class and the method.
Done when: five examples have class names and a sentence on each.
Hint
Comparator, Iterator, InputStreamReader and Collections.unmodifiableList are starting points.
Hard 3: Learn one more (90 minutes)
Choose Proxy or Memento. Read its chapter in GoF and write a one-page note with a Java example, a comparison with a nearby pattern, and a use in a design round.
Done when: the note has working code, a comparison table and one interview question with an answer.
Hint
Proxy controls access, Decorator adds behaviour.