Structural Patterns • Facade PatternMedium⏱️ ~2 min
Facade Pattern - Parking Lot Application
Domain: Parking Lot Management System
When a vehicle enters a parking lot, the system must coordinate multiple subsystems including spot allocation, payment processing, entry gate control, and ticket generation. Without a facade, the client (entry terminal software) would need to know and interact with all these components.
Before (Problem)
Client must call:
Complex initialization, error-prone, tight coupling.
spotManager.findSpot()gateController.open()ticketGenerator.create()displayPanel.show()Complex initialization, error-prone, tight coupling.
→
After (Clean)
Client calls:
Single method handles all coordination. Client decoupled from subsystems.
parkingFacade.processEntry(vehicle)Single method handles all coordination. Client decoupled from subsystems.
ParkingLotFacade
- spotManager: SpotManager
- gateController: GateController
- ticketGenerator: TicketGenerator
- displayPanel: DisplayPanel
+ processEntry(vehicle): Ticket
+ processExit(ticket): Receipt
↓ coordinates
SpotManager
+ findAvailableSpot(type): Spot
+ reserveSpot(spot): void
+ releaseSpot(spot): void
GateController
+ openEntryGate(): void
+ closeEntryGate(): void
+ openExitGate(): void
TicketGenerator
+ generate(vehicle, spot): Ticket
+ validate(ticket): boolean
DisplayPanel
+ showMessage(text): void
+ showSpotNumber(num): void
Implementation Logic
processEntry(vehicle):
spot = spotManager.findAvailableSpot(vehicle.type)
if spot is null:
displayPanel.showMessage("Lot Full")
return null
spotManager.reserveSpot(spot)
ticket = ticketGenerator.generate(vehicle, spot)
gateController.openEntryGate()
displayPanel.showSpotNumber(spot.number)
wait for vehicle sensor
gateController.closeEntryGate()
return ticket
spot = spotManager.findAvailableSpot(vehicle.type)
if spot is null:
displayPanel.showMessage("Lot Full")
return null
spotManager.reserveSpot(spot)
ticket = ticketGenerator.generate(vehicle, spot)
gateController.openEntryGate()
displayPanel.showSpotNumber(spot.number)
wait for vehicle sensor
gateController.closeEntryGate()
return ticket
Interview Tip: When explaining this example, emphasize exception handling. If spot allocation fails, the facade must not proceed to open the gate. This is where facade shines: it encapsulates the entire workflow including error cases.
💡 Key Takeaways
✓Facade coordinates entry workflow across multiple subsystems
✓Single entry point reduces client complexity from 5+ calls to 1 call
✓Subsystems remain independently testable and reusable
✓Facade handles error scenarios like full parking lot gracefully
📌 Examples
1Entry terminal calls <code>processEntry()</code> without knowing about spot allocation logic
2Exit terminal calls <code>processExit()</code> which coordinates payment, gate opening, and spot release