Bird
Raised Fist0

You need to create a new object instance from an existing one such that changes to nested mutable fields in the new object do not affect the original. Which design pattern best fits this requirement?

easy🔍 Pattern Recognition Q1 of Q15
OOP & Design Patterns - Prototype Pattern - Deep Copy vs Shallow Copy
You need to create a new object instance from an existing one such that changes to nested mutable fields in the new object do not affect the original. Which design pattern best fits this requirement?
APrototype Pattern
BSingleton Pattern
CBuilder Pattern
DFactory Pattern
Step-by-Step Solution
Solution:
  1. Step 1: Understand the problem requirement

    The problem requires creating a new object copy where nested mutable fields are independent, implying cloning with deep copy semantics.
  2. Step 2: Match with design patterns

    Prototype Pattern is specifically designed for cloning objects, including deep copying nested mutable fields, unlike Factory or Builder which create new objects from scratch or stepwise.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Prototype Pattern clones objects with deep copy -> [OK]
Quick Trick: Deep copy cloning is Prototype Pattern's core use [OK]
Common Mistakes:
MISTAKES
  • Confusing Factory with Prototype
  • Assuming Builder clones objects
Trap Explanation:
PITFALL
  • Candidates often confuse object creation (Factory) with cloning (Prototype).
Interviewer Note:
CONTEXT
  • Tests candidate's ability to recognize when cloning patterns apply.
Master "Prototype Pattern - Deep Copy vs Shallow Copy" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes