🧩 Section 2 · 16 questions

Design Patterns

16 interview questions and answers in the Design Patterns section.

Questions in this section

  1. What are design patterns
  2. What pattern categories exist
  3. What is Singleton
  4. How to implement thread-safe Singleton
  5. What is double-checked locking
  6. What are problems with Singleton
  7. Difference between Factory Method and Abstract Factory
  8. When to use Builder
  9. What is Prototype pattern
  10. When to use Strategy
  11. How is Observer implemented in Java
  12. Advantage of Decorator over inheritance
  13. Difference between State and Strategy
  14. What Proxy types exist
  15. What is Iterator pattern
  16. What anti-patterns do you know

Study navigator

A curated collection of 16 core questions covering architectural and object-oriented design patterns for Junior 🟢, Middle 🟡, and Senior 🔴 Java Developer interviews.


📋 All Questions

# Question Difficulty
1 What are design patterns ⭐
2 What pattern categories exist ⭐
3 What is Singleton ⭐⭐
4 How to implement thread-safe Singleton ⭐⭐⭐
5 What is double-checked locking ⭐⭐⭐
6 What are problems with Singleton ⭐⭐
7 Difference between Factory Method and Abstract Factory ⭐⭐
8 When to use Builder ⭐⭐
9 What is Prototype pattern ⭐⭐
10 When to use Strategy ⭐⭐⭐
11 How is Observer implemented in Java ⭐⭐
12 Advantage of Decorator over inheritance ⭐⭐
13 Difference between State and Strategy ⭐⭐⭐
14 What Proxy types exist ⭐⭐⭐
15 What is Iterator pattern ⭐⭐
16 What anti-patterns do you know ⭐⭐

🗺️ Topic Dependency Map

                    ┌──────────────────────────────────────────┐
                    │   FUNDAMENTALS (1-2)                     │
                    │   1. What are design patterns            │
                    │   2. Pattern categories                  │
                    └──────────────────┬───────────────────────┘
                                       │
             ┌─────────────────────────┼─────────────────────────┐
             ▼                         ▼                         ▼
     ┌───────────────┐         ┌───────────────┐         ┌────────────────────┐
     │ CREATIONAL    │         │ BEHAVIORAL    │         │ STRUCTURAL         │
     │ (3-9)         │         │ (10-11, 13)   │         │ (12, 14-15)        │
     │ 3. Singleton  │         │ 10. Strategy  │         │ 12. Decorator      │
     │ 4. ThreadSafe │         │ 11. Observer  │         │ 14. Proxy          │
     │    Singleton  │         │ 13. State vs  │         │ 15. Iterator       │
     │ 5. DCL        │         │     Strategy  │         │                    │
     │ 6. Problems   │         │               │         │                    │
     │    Singleton  │         │               │         │                    │
     │ 7. Factory    │         │               │         │                    │
     │ 8. Builder    │         │               │         │                    │
     │ 9. Prototype  │         │               │         │                    │
     └───────────────┘         └───────────────┘         └────────┬───────────┘
                                                                  │
                                                                  ▼
                                                        ┌────────────────────┐
                                                        │ ANTI-PATTERNS (16) │
                                                        │ 16. Anti-patterns  │
                                                        └────────────────────┘

🟢 Junior Track

| Step | Topic | Questions | Core Learning Objectives | |—|—|—|—| | 1 | Pattern Fundamentals | Q1, Q2 | Gang of Four (GoF) taxonomy, intent, and terminology | | 2 | Singleton | Q3 | Concept of global single instances and private constructors | | 3 | Factories & Builders | Q7, Q8 | Separating construction from representation | | 4 | Decorators & Iterators | Q12, Q15 | Dynamic wrapping and standard collection traversal |

🟡 Middle Track

| Step | Topic | Questions | Core Learning Objectives | |—|—|—|—| | 1 | Singleton Hazards & Spring Scope | Q6 | Testability, tightly-coupled state, and DI containers | | 2 | Strategy & Modern Lambdas | Q10 | Functional interfaces, runtime algorithm swapping, and JIT inlining | | 3 | Observer & Event Architecture | Q11 | Deprecation of java.util.Observer, Spring Events, Flow API | | 4 | Proxy Patterns | Q14 | Dynamic Proxies, CGLIB/ByteBuddy, and Spring AOP self-invocation |

🔴 Senior Track

| Step | Topic | Questions | Core Learning Objectives | |—|—|—|—| | 1 | Concurrency & JMM Internals | Q4, Q5 | Memory Barriers, volatile, CPU cache coherence, and DCL | | 2 | State Machines & Polymorphism | Q13 | Sealed Classes/Interfaces, State Registry, finite transitions | | 3 | Deep Copying & Prototype | Q9 | Reflection bypasses, serialization hazards, copy constructors | | 4 | Architectural Anti-Patterns | Q16 | God Object, Anemic Domain Model, Cargo Cult, Lava Flow |


🔗 Key Topic Relationships

Singleton Topic Cluster

Q3 (Singleton) ──> Q4 (Thread-safe) ──> Q5 (DCL) ──> Q6 (Problems)
  • Q4 ↔ Q5: Double-Checked Locking is an optimization of synchronized lazy initialization.
  • Q6 ↔ Q3: Spring ApplicationContext and DI containers eliminate mutable static Singleton anti-patterns.

Creational Patterns Cluster

Q7 (Factory Method vs Abstract) ──> Q8 (Builder) ──> Q9 (Prototype)
  • Q7 ↔ Q8: Factory manages polymorphic object creation; Builder manages complex multi-attribute parameter assembly.
  • Q8 ↔ Q9: Builder constructs new instances step-by-step; Prototype clones existing pre-configured instances.

Behavioral Patterns Cluster

Q10 (Strategy) ──> Q11 (Observer) ──> Q13 (State vs Strategy)
  • Q10 ↔ Q13: Strategy swaps independent interchangeable algorithms externally; State changes behavior based on internal state transitions.
  • Q11 ↔ Q13: Observer decouples publishers from listeners; State decouples context from complex branching logic.

Structural Patterns Cluster

Q12 (Decorator) ──> Q14 (Proxy) ──> Q15 (Iterator)
  • Q12 ↔ Q14: Decorator adds new functional responsibilities; Proxy controls or intercepts access without altering interface semantics.

🎓 Level-by-Level Knowledge Matrix

🟢 Junior

  • Patterns are reusable, standardized solutions to recurring software engineering problems.
  • 3 primary GoF categories: Creational, Structural, Behavioral.
  • Singleton provides a single JVM instance; Factory abstracts object instantiation; Builder avoids telescoping constructors.

🟡 Middle

  • Bill Pugh Singleton idiom leverages JVM classloader initialization guarantees without explicit locks.
  • DCL requires volatile to prevent instruction reordering on out-of-order CPU execution.
  • Prefer composition over inheritance to prevent class explosion ($N$ decorators vs $2^N$ subclasses).
  • Strategy implementations are simplified via Java 8+ lambdas and functional interfaces (Function, Consumer, Predicate).

🔴 Senior

  • Java Memory Model (JMM): volatile provides happens-before consistency and memory barriers.
  • JIT Monomorphic vs Megamorphic call sites: excessive strategy polymorphism (>2 implementations) disables vtable inline caching.
  • CGLIB / ByteBuddy vs JDK Dynamic Proxy: interface vs subclass generation, reflection costs, and AOP self-invocation traps.
  • Sealed classes (Java 17+) provide exhaustive pattern matching for clean state machine dispatching.

📝 Document Format Specification

Every question file strictly implements:

  • 🟢 Junior Level — Core definition, simple intuitive analogies, minimal working code.
  • 🟡 Middle Level — Under-the-hood mechanics, common mistakes, Spring integration, practical refactoring.
  • 🔴 Senior Level — JVM/JMM internals, memory safety, edge cases, production diagnostics, performance trade-offs.
  • 🎯 Interview Cheat Sheet — High-yield bullet points, common follow-up traps, red flags, and related topic links.