SOLID Boss Battle
Can you fix the God Class? Put your SOLID skills to the ultimate test by refactoring a broken, tightly-coupled ordering system.
You've learned the five pillars of SOLID. You've seen the analogies. You've looked at the comparison grids. Now, it's time to face the Boss Battle.
The Mission
Below is a piece of code that represents every developer's nightmare: The God Class. This OrderManager is a tangled mess that violates every single SOLID principle. It is rigid, fragile, and impossible to test.
Your task is not just to read it, but to Refactor it using the "Practice Now" button.
The "Spaghetti" Order Manager
Take a look at this class. Count how many times you want to scream:
class OrderManager {
// DIP Violation: Hard-coded dependency
private database = new MySQLDatabase();
// SRP Violation: Handles calculation, DB, AND notification
processOrder(order: any) {
// Calculation Logic
let total = order.price * order.quantity;
// OCP Violation: Switch statement for shipping types
if (order.shippingType === "GROUND") total += 10;
else if (order.shippingType === "AIR") total += 50;
// DB Logic
this.database.save(order);
// Notification Logic
console.log("Sending email to " + order.email);
}
}The Violation Checklist
Before you start the lab, identify exactly where the "smells" are:
- SRP: Why is the
OrderManagerresponsible for sending emails? If we switch from Email to SMS, why should the Ordering logic change? - OCP: If a new "Drone Shipping" method is added tomorrow, we have to modify the
processOrdercode. - LSP: What if we have a
DigitalOrder? It doesn't need shipping at all. Does our code handle that gracefully or will it break? - ISP: If we have a giant
IOrderActionsinterface, are we forcing small orders to implement things they don't need? - DIP: We are "married" to
MySQLDatabase. If we want to useMongoDBfor testing, we are stuck.
Your Challenge
- Extract Responsibilities: Create a
PricingService, aNotificationService, and aRepository. - Invert Dependencies: Use interfaces so the manager doesn't care which database or which notification method is used.
- Open for Extension: Make the shipping logic "pluggable."
Congratulations! You've completed the full Learning by Doing LLD course. You now have the mental models and the tools to build software that is professional, scalable, and beautiful.
Practice what you just read.