Skip to content
Ameen Abozaid
JNR-004

SOLID principles

  • 7 Lessons
  • 72 min of reading
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • SRP
  • OCP
  • LSP
  • ISP
  • DIP with practical Java examples

Learn first

72 min of reading · 3 h of practice (estimate)

Learning outcomes

  1. State each SOLID principle and identify a violation in Java code.
  2. Refactor a violation using composition, interfaces, or class extraction.
  3. Explain how dependency inversion enables testing and is the foundation of Spring's DI.
  4. Recognize when applying a principle would add needless complexity.

Lessons

  1. 01Single Responsibility10 min read
  2. 02Open/Closed11 min read
  3. 03Liskov Substitution10 min read
  4. 04Interface Segregation10 min read
  5. 05Dependency Inversion10 min read
  6. 06SOLID with judgment11 min read
  7. 07Demo: dependency inversion for a payment provider10 min read

Lab and exercises

Lab

JNR-004-LAB1 · Pluggable pricing

Outcomes: 1–4 · Time: ~2 h Refactor Shop Core so tax rules and payment providers are injected through interfaces. Add a new tax rule without modifying existing classes. Write a fake payment provider for tests. Acceptance: core has no dependency on concrete providers; a new tax rule is added by adding a class only; tests run without a network.

Exercises (assessment: design quiz with code snippets plus the lab; six exercises)

IDOutcomeTask
JNR-004-EX011State each SOLID letter in one sentence each.
JNR-004-EX021A ReportService queries a database, formats a PDF, and emails it. Which principle does it break?
JNR-004-EX032Refactor if (type.equals("PERCENT")) ... else if (type.equals("FIXED")) ... so a new type needs no edit to existing code.
JNR-004-EX041A ReadOnlyList extends MyList throws UnsupportedOperationException in add. Which principle is broken and how do you fix it?
JNR-004-EX053Make CheckoutService depend on a PaymentProvider interface and test it with a fake.
JNR-004-EX064Give an example where an interface with a single implementation is needless. Say what you would do instead.
Show solutions
  • EX01 S: one reason to change. O: extend without editing. L: subtypes work wherever the parent does. I: small, focused interfaces. D: depend on interfaces, not concrete classes.
  • EX02 SRP. Split into ReportQuery, PdfFormatter, ReportMailer.
  • EX03 Introduce interface Discount { Money apply(Money p); } with a class per type; a new type is a new class (OCP).
  • EX04 LSP. Do not inherit; separate ReadableList and MutableList interfaces (ISP helps too).
  • EX05 CheckoutService(PaymentProvider p); test with class FakePayments implements PaymentProvider { ... } returning a fixed receipt.
  • EX06 A PriceFormatter interface with only one class and no test need. Use the class directly; extract an interface when a second implementation or test double is needed.
  • The assessment design quiz covers outcome 1 and 4 further; outcomes 2–3 are covered by the lab.

Quiz

Outcomes: O1 state principles and find violations · O2 refactor · O3 dependency inversion and DI · O4 judgment.

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. 1The "S" in SOLID stands for:O1
  2. 2A class that changes for tax, email, and database reasons breaks:O1
  3. 3An override that throws UnsupportedOperationException usually breaks:O1
  4. 4To follow Open/Closed for a new discount type you:O2
  5. 5A huge interface with many unused methods is fixed by:O2
  6. 6Dependency inversion means high-level code depends on:O3
  7. 7Constructor injection helps testing because:O3
  8. 8Spring's dependency injection is built on:O3
  9. 9An interface with one implementation and no need to swap it is:O4
  10. 10When should you apply a principle?O4

Practical part

Six exercises JNR-004-EX01 to EX06 plus the lab, with the design quiz as the written assessment. Pass: at least 8 of 10 quiz answers, five exercises correct, and a core with no dependency on a concrete provider.

Common mistakes

An interface for every class; "SRP" meaning one method per class; LSP violations via throwing UnsupportedOperationException.

Assessment

Design quiz with code snippets plus the lab.

Career relevance

Design discussions at every level; architecture styles (MID-059).

Type to search levels, phases, modules and lessons.