Roadmap › Low-Level Design (LLD) — Object Design, UML and Classic Problems in C# › Module: Foundations of Object Design
Lessons in this course (8)
3. Applying SOLID and design patterns in LLD
Intermediate★ Must knowLesson 3 of 8
| Situation in a design | Pattern | Example |
|---|---|---|
| Behaviour varies by type/rule and can be swapped | Strategy | Fee calculation per vehicle type; payment methods; pricing |
| Creating objects depends on input | Factory / Abstract Factory | VehicleFactory.Create("CAR", number) |
| Object behaves differently in each lifecycle state | State | Vending machine (Idle → HasMoney → Dispensing), order status |
| Notify many parts when something happens | Observer | Display boards update when spots change; notify users |
| Exactly one shared instance | Singleton (prefer DI singleton lifetime) | ParkingLot instance, configuration |
| Add features without changing a class | Decorator | Add-ons to a pizza/coffee price; logging around a service |
| Step-by-step construction of complex objects | Builder | Building a Booking or HTTP request |
| Chain of handlers each deciding to handle or pass on | Chain of Responsibility | ATM cash dispenser (2000 → 500 → 100 notes), approval workflows |
| Encapsulate a request as an object | Command | Undo/redo, queued elevator requests |
| Simplify a complex subsystem | Facade | BookingFacade 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);
}