Skip to content
Ameen Abozaid
JNR-005

Design patterns introduction

  • 10 Lessons
  • 108 min of reading
  • 4 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Factory
  • Builder
  • Strategy
  • Adapter
  • Decorator
  • Observer
  • Repository

Learn first

108 min of reading · 4 h of practice (estimate)

Learning outcomes

  1. Implement Factory, Builder, Strategy, Adapter, Decorator, Observer, and Repository in Java.
  2. Describe the problem each pattern solves and choose one for a given scenario.
  3. Recognize these patterns in the JDK and Spring.
  4. Avoid applying a pattern where a simple function or class suffices.

Lessons

  1. 01What patterns are (and are not)11 min read
  2. 02Factory10 min read
  3. 03Builder11 min read
  4. 04Strategy10 min read
  5. 05Adapter11 min read
  6. 06Decorator11 min read
  7. 07Observer11 min read
  8. 08Repository11 min read
  9. 09Spotting patterns in Spring11 min read
  10. 10Demo: replacing a type switch with Strategy11 min read

Lab and exercises

Lab

JNR-005-LAB1 · Patterns in Shop Core

Outcomes: 1–4 · Time: ~2.5 h Add four patterns to Shop Core: Strategy for shipping, Adapter for a third-party currency API, Decorator for logging or caching a price lookup, Observer for "order placed" notifications. Acceptance: each pattern is justified in the README (problem, why this pattern, one alternative considered); tests for each; no pattern where a simple function would do.

Exercises (assessment: scenario quiz plus the lab; eight exercises)

IDOutcomeTask
JNR-005-EX011Write a DiscountFactory.of(String code) that returns a Discount; unknown code throws.
JNR-005-EX021Write a builder for Order with customer, lines, and an optional coupon; build() rejects an empty order.
JNR-005-EX031Implement ShippingStrategy as two classes and one lambda, and pick one by name.
JNR-005-EX041Write an adapter from VendorApi.rate(String, String) to your CurrencyRates interface.
JNR-005-EX051Write a LoggingPriceLookup decorator, and a ProductRepository in-memory implementation.
JNR-005-EX061Implement an OrderPlaced listener list with two listeners.
JNR-005-EX072Which pattern solves each: many constructor parameters; swap tax algorithm; wrap a legacy API; add caching; notify on events; hide SQL.
JNR-005-EX083, 4Name one JDK and one Spring example for three patterns. Then describe a case where a pattern is needless.
Show solutions
  • EX01 switch (code) { case "SUMMER10" -> new Percentage(10); ... default -> throw new IllegalArgumentException(code); }.
  • EX02 Static nested Builder with fluent setters; build() checks lines.isEmpty() and returns an immutable Order.
  • EX03 ShippingStrategy standard = o -> Money.eur(499); and a pick via Map<String, ShippingStrategy>.
  • EX04 class VendorRatesAdapter implements CurrencyRates { public Money convert(...) { double r = api.rate(...); ... } }.
  • EX05 The decorator holds a PriceLookup inner, logs, then calls inner.priceOf; the repository wraps a Map<Sku, Product>.
  • EX06 List<OrderListener>; loop and call onPlaced. Do not depend on order.
  • EX07 Builder; Strategy; Adapter; Decorator; Observer; Repository.
  • EX08 Examples: Factory List.of / Spring BeanFactory; Strategy Comparator / JdbcTemplate callbacks; Decorator BufferedReader / Spring proxies. Needless: Builder for a two-field object.
  • Outcome 2 and 4 are also covered by the scenario quiz in the assessment and quiz Q07–Q10.

Quiz

Outcomes: O1 implement the patterns · O2 problem and choice · O3 patterns in JDK and Spring · O4 avoid needless patterns.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1A factory method:O1
  2. 2A Decorator implements:O1
  3. 3Which pattern removes an if/else that chooses an algorithm?O2
  4. 4Which pattern fits a third-party API with a different shape?O2
  5. 5Many optional constructor parameters suggest:O2
  6. 6"Notify email and stock when an order is placed" suggests:O2
  7. 7Comparator passed to sort is an example of:O3
  8. 8BufferedReader wrapping a Reader is an example of:O3
  9. 9A Builder for an object with two fields is:O4
  10. 10Which statement is right?O4

Practical part

Eight exercises JNR-005-EX01 to EX08 plus the lab, with the scenario quiz (which pattern and why) as the written assessment. Pass: at least 8 of 10 quiz answers, seven exercises correct, and a README that justifies each lab pattern.

Common mistakes

Pattern-driven design; Builder for two-field objects; Observer with hidden ordering assumptions.

Assessment

Scenario quiz (which pattern, and why) plus the lab.

Career relevance

Shared vocabulary for design reviews; Observer grows into event-driven design (MID-026).

Type to search levels, phases, modules and lessons.