A Framework for Object-Oriented Design
When faced with an OOD problem in an interview, jumping straight into code is a common mistake. A structured approach ensures you cover all bases—from ambiguous requirements to clean, modular code.
The 4-Step Framework
This framework provides a repeatable structure to tackle any design problem, ensuring you demonstrate both design thinking and technical proficiency.
Step 1: Clarify the Requirements (5-10 mins)
OOD problems are often intentionally vague. Your goal is to narrow the scope and identify the exact boundaries of the system.
- Identify Actors: Who or what interacts with the system? (e.g., Guest, Registered Member, System System).
- Determine Use Cases: What can these actors do? (e.g., "A customer can book a parking spot").
- Define Constraints: What are the limits? (e.g., "The parking lot has 4 floors, and each floor has 100 spots").
- Ask "What if" Questions: Handle edge cases early. (e.g., "What happens if the parking lot is full?").
[!NOTE] Focus on the Use Cases first. They will reveal the core functionality your design must support.
Step 2: Define the Core Objects (5-10 mins)
Break down your requirements into "nouns" (objects) and "verbs" (methods). This is the foundation of your class structure.
- Identify Key Classes: Look for the main entities (e.g.,
ParkingLot,Vehicle,Ticket). - Define Properties: What data does each class hold? (e.g.,
Vehiclehas alicenseNumber). - Define Methods: What actions can each class perform? (e.g.,
ParkingLotcanprocessEntry()).
Relationship Mapping Concept:
Step 3: Map the Relationships (10 mins)
Now, determine how your core objects interact. This is where you decide on inheritance vs. composition.
- Inheritance ("is-a"): Does one class share common traits with another? (e.g., a
Caris aVehicle). - Composition ("has-a"): Does one class contain another? (e.g., a
ParkingFloorhas manyParkingSpots). - Dependency: Does one class use another temporarily? (e.g., a
PaymentProcessoruses aBankAPI).
| Relationship Type | Example | Best Practice |
|---|---|---|
| Inheritance | Car → Vehicle | Keep hierarchy shallow. |
| Composition | Floor → Lot | Prefer composition for better flexibility. |
| Association | Member → Ticket | Use when objects just need to "know" each other. |
Step 4: Implement the Logic (15-20 mins)
Finally, translate your design into code. Focus on the core logic and use design patterns to handle complexity.
- Handle State: Use private fields to encapsulate internal state.
- Implement Patterns:
- Singleton: For global managers like
ParkingLot. - Factory: For creating different types of objects (e.g.,
VehicleFactory). - Strategy: For interchangeable logic (e.g., different
PaymentMethods).
- Singleton: For global managers like
- Error Handling: Don't forget to handle invalid inputs or system failures.
Conclusion: Algorithm vs. OOD Interview
| Feature | Algorithm Interview | OOD Interview |
|---|---|---|
| Primary Goal | Efficiency (Time/Space) | Maintainability & Flexibility |
| Focus | How to solve a problem fast? | how to structure a system? |
| Key Tools | Data Structures, Recursion | SOLID, Design Patterns, Classes |
In the next section, we will apply this framework to a classic problem: Designing a Parking Lot.