Bird
0
0

Which of the following is a correct way to define a Null Object class in a system design context?

easy🧠 Conceptual Q3 of 15
LLD - Behavioral Design Patterns — Part 2
Which of the following is a correct way to define a Null Object class in a system design context?
Aclass NullLogger { log(message) { throw new Error('Null'); } }
Bclass NullLogger { log(message) { console.error(message); } }
Cclass NullLogger { log(message) { return null; } }
Dclass NullLogger { log(message) { /* do nothing */ } }
Step-by-Step Solution
Solution:
  1. Step 1: Understand Null Object behavior

    It should implement the interface with safe, do-nothing methods.
  2. Step 2: Evaluate options

    class NullLogger { log(message) { /* do nothing */ } } does nothing safely. class NullLogger { log(message) { throw new Error('Null'); } } throws error, C returns null (not safe), D logs error (not silent).
  3. Final Answer:

    class NullLogger { log(message) { /* do nothing */ } } -> Option D
  4. Quick Check:

    Null Object method = safe no-op [OK]
Quick Trick: Null Object methods do nothing safely [OK]
Common Mistakes:
MISTAKES
  • Throwing errors inside Null Object
  • Returning null instead of safe object
  • Logging errors in Null Object methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes