Lessons in this course (8)

3. Applying SOLID and design patterns in LLD

Intermediate★ Must knowLesson 3 of 8
Situation in a designPatternExample
Behaviour varies by type/rule and can be swappedStrategyFee calculation per vehicle type; payment methods; pricing
Creating objects depends on inputFactory / Abstract FactoryVehicleFactory.Create("CAR", number)
Object behaves differently in each lifecycle stateStateVending machine (Idle → HasMoney → Dispensing), order status
Notify many parts when something happensObserverDisplay boards update when spots change; notify users
Exactly one shared instanceSingleton (prefer DI singleton lifetime)ParkingLot instance, configuration
Add features without changing a classDecoratorAdd-ons to a pizza/coffee price; logging around a service
Step-by-step construction of complex objectsBuilderBuilding a Booking or HTTP request
Chain of handlers each deciding to handle or pass onChain of ResponsibilityATM cash dispenser (2000 → 500 → 100 notes), approval workflows
Encapsulate a request as an objectCommandUndo/redo, queued elevator requests
Simplify a complex subsystemFacadeBookingFacade hides seats, payment, notifications
csharp
// Strategy + Open/Closed: add a new vehicle fee rule without touching existing code
public interface IFeeStrategy { decimal Calculate(TimeSpan duration); }
public sealed class CarFee : IFeeStrategy { public decimal Calculate(TimeSpan d) => 40 + Math.Max(0, Math.Ceiling(d.TotalHours) - 1) * 30; }
public sealed class BikeFee : IFeeStrategy { public decimal Calculate(TimeSpan d) => 20 + Math.Max(0, Math.Ceiling(d.TotalHours) - 1) * 10; }

public sealed class FeeCalculator(IReadOnlyDictionary<VehicleType, IFeeStrategy> strategies)
{
    public decimal For(VehicleType type, TimeSpan duration) => strategies[type].Calculate(duration);
}