Course contents

Design patterns and SOLID: the must-knows

SOLID with a Java violation and fix for each principle, the thread-safe singletons and how they break, the creational, structural and behavioural patterns interviewers actually ask about, where they live in the JDK, and how to use them in an LLD round without over-engineering.

PremiumMust know for interviewsLesson25 min readCoreReviewed Oct 2026 · Java 21
On this page
  1. Picture it first: from problem to pattern
  2. SOLID, one principle at a time
  3. Creational patterns: singleton, factory, builder
  4. Structural patterns: four wrappers with different intent
  5. Behavioural patterns: moving the logic that varies
  6. Patterns already in the JDK
  7. Pitfalls and senior probes
  8. Using patterns in an LLD round
  9. How to say it in the interview

🧩 What is this sheet?

It's the design half of a Java interview on one page: the five SOLID principles with a violation and a fix for each, the dozen patterns that come up again and again, where they already live in the JDK, and how to use them in a low-level design round. A design pattern acts like a named recipe: "decorator" tells another engineer in one word that you're wrapping an object to add behaviour without touching it. I picked these items because they are the ones I see asked, probed and misused most in Java rounds.

For this sheet, we'll go principle by principle, then pattern by pattern, with the smallest Java that shows each one and the follow-up question an interviewer asks next. The first half is the core you can't skip (SOLID, singletons, factories, builders, the structural wrappers). The second half covers behavioural patterns, the JDK map, anti-patterns, and how to put it all together in a parking-lot-style LLD round.

Below the line (out of scope)

The 30-second answer

SOLID is five rules for code that's cheap to change: one reason to change per class, extend by adding code rather than editing it, subtypes that honour the parent's contract, small client-specific interfaces, and high-level policy depending on abstractions it owns. Patterns are named solutions to recurring change problems: creational ones control how objects get made (enum or holder singleton, factory, builder), structural ones wrap or compose objects (adapter changes an interface, decorator adds behaviour, proxy controls access, facade simplifies), and behavioural ones move varying logic into objects (strategy, observer, template method, command, chain, state). In an LLD round I name a pattern only where it removes a real axis of change, like pricing rules or vehicle types, and I say which change it protects against.

What you’ll learn

  • Explain each SOLID principle with a small Java violation and fix, including LSP beyond Rectangle/Square and DIP vs DI
  • Write a thread-safe singleton three ways (enum, holder idiom, volatile double-checked locking) and say how reflection and serialization break it
  • Tell apart factory method vs abstract factory, and adapter, decorator, proxy and facade by intent, not shape
  • Use strategy, observer, template method, command, chain of responsibility and state, and name where each lives in the JDK
  • Spot the anti-patterns (god class, singleton abuse, anaemic models) and pick patterns in an LLD round only where they remove a real axis of change

Interview radar

Asked very often: Write a thread-safe singleton. Explain SOLID with examples. Strategy vs state. Decorator vs proxy. Builder and why. Senior rounds probe: How reflection and serialization break a singleton, LSP beyond Rectangle/Square, DIP vs dependency injection, why Spring's @Transactional fails on self-invocation, observer memory leaks. Production angle: Singletons that make tests share state, listener lists that leak, god services nobody dares touch, and pattern-heavy code where a lambda would do.

01Picture it first: from problem to pattern

Many candidates memorise patterns as a list of 23 names. That's backwards. Interviewers give you a problem ("pricing differs by vehicle type and changes every quarter"), and you're expected to recognise which force is at play and reach for the matching pattern. If you can read the map below left to right, you can handle most design follow-ups.

The diagram groups the common problems by what varies, and points each one at the pattern that isolates that variation.

Fig. 1

A pattern picker: on the left, problems grouped as creation, wrapping and behaviour; on the right, the pattern that solves each, such as many optional fields leading to builder, incompatible interface leading to adapter, add behaviour by wrapping leading to decorator, swappable algorithm leading to strategy and lifecycle rules leading to state

A pattern picker: on the left, problems grouped as creation, wrapping and behaviour; on the right, the pattern that solves each, such as many optional fields leading to builder, incompatible interface leading to adapter, add behaviour by wrapping leading to decorator, swappable algorithm leading to strategy and lifecycle rules leading to state

Notice the shape of the map. Every arrow starts with something that varies or something you want to hide. If you can't name that thing in your design, you don't need the pattern yet.

02SOLID, one principle at a time

SOLID is five principles collected by Robert C. Martin. Interviewers rarely want the acronym recited; they want a small violation, the fix, and why the fix is cheaper to change. Here's the can't-skip list, then code for the ones people fumble.

  • S, Single Responsibility: a class should have one reason to change, meaning one actor or stakeholder who asks for changes. "Does one thing" is a weaker reading; the useful test is "who would file the ticket that changes this class?"
  • O, Open/Closed: open for extension, closed for modification. Adding a new payment type should mean adding a class, not editing a switch in five places.
  • L, Liskov Substitution: any subtype must work wherever the parent is expected, honouring its contract: no stronger preconditions, no weaker postconditions, no new surprise exceptions, invariants preserved.
  • I, Interface Segregation: clients shouldn't depend on methods they don't use. Prefer several role interfaces (Readable, Writable) over one fat one.
  • D, Dependency Inversion: high-level policy depends on abstractions it owns, and low-level details implement them. OrderService depends on OrderRepository, and the MySQL class implements it.

SRP: the invoice that does everything

This class has three reasons to change: finance changes the tax rule, design changes the PDF, and ops changes the mail provider.

// Violation: three actors, one class
class InvoiceService {
    Money total(Invoice inv) { /* tax rules */ }
    byte[] renderPdf(Invoice inv) { /* layout */ }
    void email(Invoice inv, String to) { /* SMTP */ }
}

// Fix: one reason to change each, composed by a thin coordinator
class InvoiceCalculator { Money total(Invoice inv) { /* tax rules */ } }
class InvoiceRenderer   { byte[] pdf(Invoice inv) { /* layout */ } }
class InvoiceMailer     { void send(byte[] pdf, String to) { /* SMTP */ } }

The fix isn't "more classes is better". It's that a tax change now can't break PDF rendering, and each piece gets a focused test.

OCP: the switch that grows forever

The classic violation is a switch on a type code that every new feature edits. The fix moves each case into its own class behind an interface, so new behaviour is new code.

// Violation: every new method edits this switch
double fee(String method, double amount) {
    return switch (method) {
        case "CARD" -> amount * 0.02;
        case "UPI"  -> 0;
        default     -> throw new IllegalArgumentException(method);
    };
}

// Fix: extend by adding an implementation
interface FeePolicy { double fee(double amount); }
record CardFee() implements FeePolicy { public double fee(double a) { return a * 0.02; } }
record UpiFee()  implements FeePolicy { public double fee(double a) { return 0; } }

Premium lesson

Keep reading with the Java Pass

The rest of this lesson is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.

  • Creational patterns: singleton, factory, builder
  • Structural patterns: four wrappers with different intent
  • Behavioural patterns: moving the logic that varies
  • Patterns already in the JDK
  • Pitfalls and senior probes
  • Using patterns in an LLD round
  • How to say it in the interview

From ₹199 · one-time payment · No auto-renewal

Interview questions for this topicComing soon

Further reading (8)