Explain SOLID with a short example for each letter.
Answer
- S, Single Responsibility:
InvoiceCalculator computes; InvoiceRepository saves; InvoicePdf renders. - O, Open/Closed: add a new
DiscountStrategy class instead of editing a big if/elif. - L, Liskov Substitution: any
Storage subclass must keep the same contract (no surprise exceptions, same meaning). - I, Interface Segregation:
Readable and Writable instead of one fat FileLike interface. - D, Dependency Inversion:
OrderService depends on a PaymentGateway abstraction; the Stripe implementation is injected. - Close with why: easier change, testing and teamwork. Do not over-engineer small code.
Practice this question in Repaso