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.

May 1, 20263 min read6 / 6

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:

  1. SRP: Why is the OrderManager responsible for sending emails? If we switch from Email to SMS, why should the Ordering logic change?
  2. OCP: If a new "Drone Shipping" method is added tomorrow, we have to modify the processOrder code.
  3. LSP: What if we have a DigitalOrder? It doesn't need shipping at all. Does our code handle that gracefully or will it break?
  4. ISP: If we have a giant IOrderActions interface, are we forcing small orders to implement things they don't need?
  5. DIP: We are "married" to MySQLDatabase. If we want to use MongoDB for testing, we are stuck.

Your Challenge

  1. Extract Responsibilities: Create a PricingService, a NotificationService, and a Repository.
  2. Invert Dependencies: Use interfaces so the manager doesn't care which database or which notification method is used.
  3. 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.

The Grand Refactor: Order ManagementBoss Battle: The Reference Quiz
2 exercises