Bird
Raised Fist0

In designing a parking lot system using OOP, which design pattern best encapsulates the creation of different types of parking spots (e.g., compact, large, handicapped) to allow easy extension without modifying existing code?

easy🔍 Pattern Recognition Q1 of Q15
OOP & Design Patterns - Design a Parking Lot - LLD with OOP (Classes, Patterns, Extensibility)
In designing a parking lot system using OOP, which design pattern best encapsulates the creation of different types of parking spots (e.g., compact, large, handicapped) to allow easy extension without modifying existing code?
ASingleton pattern to ensure only one instance of each parking spot type exists
BObserver pattern to notify spots when a vehicle arrives
CFactory pattern to create parking spot objects based on type at runtime
DDecorator pattern to add features to parking spots dynamically
Step-by-Step Solution
Solution:
  1. Step 1: Identify the need for object creation flexibility

    The system must create different parking spot types without changing existing code, which calls for a creational pattern.
  2. Step 2: Evaluate each pattern's purpose

    Singleton restricts instances but doesn't help with multiple spot types; Observer is for event notification, not creation; Decorator adds behavior dynamically but doesn't create new types.
  3. Step 3: Recognize Factory pattern's role

    Factory pattern encapsulates object creation logic and allows easy extension by adding new spot types without modifying client code.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Factory pattern is the standard for flexible object creation [OK]
Quick Trick: Factory pattern creates objects flexibly [OK]
Common Mistakes:
MISTAKES
  • Confusing Singleton with Factory for object creation
  • Thinking Observer manages object creation
  • Assuming Decorator creates new object types
Trap Explanation:
PITFALL
  • Candidates often pick Singleton thinking it controls instance creation, but it limits instances rather than types.
Interviewer Note:
CONTEXT
  • Tests understanding of creational patterns and extensibility in OOP design.
Master "Design a Parking Lot - LLD with OOP (Classes, Patterns, Extensibility)" 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