Section 2 · 16 questions

Design Patterns

16 interview questions and answers in the Design Patterns section.

English Design Patterns Source Markdown
Language versions: English Russian Ukrainian

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

16 questions for Middle Java Developer interview preparation.


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

                    +--------------------------------------------------+
                    |   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 |
                                                            +-------------------+

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