← Back to Java

Design Patterns in Java

JavaMidjava

The Question

What are design patterns in Java?

What a Strong Answer Covers

  • reusable solutions to common design problems
  • Creational/Structural/Behavioral categories
  • Singleton/Factory/Builder
  • Observer/Strategy/Decorator examples
  • best practices

Senior-Level Answer

Design patterns are proven, reusable solutions to commonly recurring software design problems. They are not code libraries or algorithms — they are templates describing how to structure classes and objects to solve a specific type of design challenge. The term was popularized by the Gang of Four (GoF) book (Gamma, Helm, Johnson, Vlissides, 1994), which cataloged 23 fundamental patterns.

The GoF patterns are organized into three categories. Creational patterns address object creation: Singleton (one instance), Factory Method (defer instantiation to subclasses), Abstract Factory (families of related objects), Builder (step-by-step construction of complex objects), and Prototype (clone-based creation). Structural patterns address class and object composition: Adapter (compatibility bridge between interfaces), Decorator (add behavior dynamically without subclassing), Facade (simplified interface to a complex subsystem), Proxy (controlled access to another object), and Composite (tree structures of objects). Behavioral patterns address communication and responsibility: Strategy (interchangeable algorithms), Observer (event notification), Command (encapsulate requests as objects), Iterator (sequential access), Template Method (define skeleton, defer steps to subclasses), and Chain of Responsibility.

Java's standard library embeds many of these patterns. java.util.Iterator is the Iterator pattern. java.io streams use Decorator heavily. java.lang.Runtime.getRuntime() is Singleton. The Collections.sort(list, comparator) API is Strategy. java.util.Observable (deprecated) was Observer.

Patterns have tradeoffs. Singleton introduces global state, making testing harder. Deep Decorator chains become hard to debug. Overusing patterns adds accidental complexity. The key is recognizing the problem a pattern solves and applying it deliberately, not pattern-matching for its own sake.

In interviews, naming patterns is table stakes. Demonstrating which problem each pattern solves and when not to use it distinguishes senior-level thinking.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Names the three GoF categories with at least two patterns per category, and gives one concrete example from the Java standard library.

3/3 — Strong Answer

Explains what a pattern is vs. an algorithm/library, names the three categories with representative patterns, provides multiple JDK examples, and discusses a tradeoff or anti-pattern context.

Common Mistakes

  • Listing pattern names without explaining what problem each solves — naming is not the same as understanding.
  • Not being able to give a concrete example from the JDK or real code for the patterns mentioned.
  • Treating design patterns as always the right solution — experienced developers know when patterns introduce unnecessary complexity.

Follow-Up Questions

  • What is the difference between Factory Method and Abstract Factory patterns? — Factory Method uses inheritance — a subclass decides which class to instantiate. Abstract Factory uses composition — an object creates families of related products without specifying concrete classes.
  • How does the Decorator pattern differ from inheritance for extending behavior? — Decorator composes behavior at runtime by wrapping objects, avoiding the class explosion of static inheritance and adhering to the Open/Closed Principle.
  • Where does the Observer pattern appear in modern Java frameworks? — Spring's ApplicationEvent/ApplicationListener, RxJava/Project Reactor (reactive streams), and java.beans.PropertyChangeListener are all Observer-based.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout