Design Patterns
16 interview questions and answers in the Design Patterns section.
Questions in this section
- What are Design Patterns?
- What Pattern Categories Exist?
- What is Singleton?
- How to Implement Thread-Safe Singleton?
- What is Double-Checked Locking?
- What Are Problems with Singleton?
- Difference Between Factory Method and Abstract Factory?
- When to Use Builder?
- What is Prototype Pattern?
- When to Use Strategy?
- How is Observer Implemented in Java?
- Advantage of Decorator over Inheritance?
- Difference Between State and Strategy?
- What Proxy Types Exist?
- What is Iterator Pattern?
- What Anti-Patterns Do You Know?
Study navigator
16 questions for Middle Java Developer interview preparation.
All Questions
Topic Dependency Map
+--------------------------------------------------+
| BASICS (1-2) |
| 1. What are design patterns |
| 2. Pattern categories |
+------------------------+-------------------------+
|
+--------------------------------+--------------------------------+
v v v
+---------------+ +---------------+ +-------------------+
| CREATIONAL | | BEHAVIORAL | | STRUCTURAL |
| (3-9) | | (10-11, 13) | | (12, 14-15) |
| 3. Singleton | | 10. Strategy | | 12. Decorator |
| 4. Thread-safe | | 11. Observer | | 14. Proxy |
| Singleton | | 13. State vs | | 15. Iterator |
| 5. DCL | | Strategy | | |
| 6. Problems | | | | |
| Singleton | | | | |
| 7. Factory | | | | |
| 8. Builder | | | | |
| 9. Prototype | | | | |
+---------------+ +---------------+ +--------+----------+
|
v
+-------------------+
| ANTI-PATTERNS (16)|
| 16. Anti-patterns |
+-------------------+
Recommended Study Order
Junior Level (weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | What are patterns | Q1, Q2 | Why needed, GoF categories |
| 2 | Singleton | Q3 | Single instance, where used |
| 3 | Factory and Builder | Q7, Q8 | Object creation |
| 4 | Decorator and Iterator | Q12, Q15 | Wrapping, iteration |
Middle Level (weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Singleton Problems | Q6 | Testability, DI, scaling |
| 2 | Strategy | Q10 | When to use, JIT optimizations |
| 3 | Observer | Q11 | java.util.Observer deprecated, Flow API |
| 4 | Proxy | Q14 | JDK vs CGLIB, self-invocation |
Senior Level (weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Thread-safe Singleton | Q4, Q5 | Bill Pugh, DCL, volatile, JMM |
| 2 | State vs Strategy | Q13 | State machine, transitions via Registry |
| 3 | Prototype | Q9 | clone() issues, copy constructors |
| 4 | Anti-patterns | Q16 | God Object, Cargo Cult, Spaghetti |
Key Topic Relationships
Singleton Topic
Q3 (Singleton) -> Q4 (Thread-safe) -> Q5 (DCL) -> Q6 (Problems)
Key relationships:
- Q4 <-> Q5: DCL — one way to implement thread-safe Singleton
- Q6 <-> Q3: DI container (Spring) solves Singleton problems
Creational Patterns Topic
Q7 (Factory) -> Q8 (Builder) -> Q9 (Prototype)
Key relationships:
- Q7 <-> Q8: Factory = multiple ways to create, Builder = step-by-step creation
- Q8 <-> Q9: Builder = construction, Prototype = copying
Behavioral Patterns Topic
Q10 (Strategy) -> Q11 (Observer) -> Q13 (State vs Strategy)
Key relationships:
- Q10 <-> Q13: Strategy = algorithm changed externally, State = state changed internally
- Q11 <-> Q13: Observer = notify subscribers, State = delegate behavior
Structural Patterns Topic
Q12 (Decorator) -> Q14 (Proxy) -> Q15 (Iterator)
Key relationships:
- Q12 <-> Q14: Decorator adds behavior, Proxy controls access
- Q14 <-> Q6: Proxy solves Singleton problems (self-invocation, testability)
Cheat Sheet: What to Know for Each Level
Junior
- Patterns — proven solutions to typical tasks
- 3 categories: creational, structural, behavioral
- Singleton = one instance, Factory = object creation
- Builder = step-by-step creation of complex objects
Middle
- Bill Pugh Singleton = static inner class, thread-safe without synchronized
- DCL = double check + volatile, synchronized optimization
- DI container (Spring) solves Singleton problems
- Strategy = algorithm via interface, Observer = event subscription
- Decorator vs inheritance: n decorators instead of 2^n classes
Senior
- JMM: volatile = happens-before guarantee, DCL without volatile = broken
- Megamorphic call threshold = 5 in HotSpot, inline cache optimization
- java.util.Observable deprecated (Java 9), use Flow API
- JDK Proxy ~= CGLIB performance (JDK 8u40+), Spring Boot default = CGLIB
- clone() bypasses constructor, copy constructor is more reliable
- State transitions via Registry/Factory, not through new State()
File Format
Each file contains:
- Junior Level — basic understanding, simple analogies, examples
- Middle Level — internals, typical mistakes, practical examples
- Senior Level — deep dive, edge cases, production experience, monitoring
- Interview Cheat Sheet — key points, common questions, red flags, related topics