Design patterns introduction
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
- Implement Factory, Builder, Strategy, Adapter, Decorator, Observer, and Repository in Java.
- Describe the problem each pattern solves and choose one for a given scenario.
- Recognize these patterns in the JDK and Spring.
- Avoid applying a pattern where a simple function or class suffices.
Lessons
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)
| ID | Outcome | Task |
|---|---|---|
| JNR-005-EX01 | 1 | Write a DiscountFactory.of(String code) that returns a Discount; unknown code throws. |
| JNR-005-EX02 | 1 | Write a builder for Order with customer, lines, and an optional coupon; build() rejects an empty order. |
| JNR-005-EX03 | 1 | Implement ShippingStrategy as two classes and one lambda, and pick one by name. |
| JNR-005-EX04 | 1 | Write an adapter from VendorApi.rate(String, String) to your CurrencyRates interface. |
| JNR-005-EX05 | 1 | Write a LoggingPriceLookup decorator, and a ProductRepository in-memory implementation. |
| JNR-005-EX06 | 1 | Implement an OrderPlaced listener list with two listeners. |
| JNR-005-EX07 | 2 | Which pattern solves each: many constructor parameters; swap tax algorithm; wrap a legacy API; add caching; notify on events; hide SQL. |
| JNR-005-EX08 | 3, 4 | Name 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
Builderwith fluent setters;build()checkslines.isEmpty()and returns an immutableOrder. - EX03
ShippingStrategy standard = o -> Money.eur(499);and a pick viaMap<String, ShippingStrategy>. - EX04
class VendorRatesAdapter implements CurrencyRates { public Money convert(...) { double r = api.rate(...); ... } }. - EX05 The decorator holds a
PriceLookup inner, logs, then callsinner.priceOf; the repository wraps aMap<Sku, Product>. - EX06
List<OrderListener>; loop and callonPlaced. Do not depend on order. - EX07 Builder; Strategy; Adapter; Decorator; Observer; Repository.
- EX08 Examples: Factory
List.of/ SpringBeanFactory; StrategyComparator/JdbcTemplatecallbacks; DecoratorBufferedReader/ 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.
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).