Software Engineering - spinningideas/resources GitHub Wiki

Engineering Management

Laws

Learning

Patterns

SOLID

SOLID is a mnemonic for five object-oriented design principles introduced by Robert C. Martin in his 2000 paper Design Principles and Design Patterns and later popularized in Agile Software Development, Principles, Patterns, and Practices. Michael Feathers coined the acronym itself around 2004. Applying these principles reduces software rot, lowers coupling, and makes code easier to understand, test, and maintain.

Benefits

  • Understandability: Each module has a clear, focused purpose.
  • Maintainability: Changes are localized, reducing the risk of regressions.
  • Testability: Small, decoupled components can be tested in isolation.
  • Flexibility: New behavior can be added without modifying proven code.

Principles

  • Single Responsibility Principle (SRP) - A class should have only one reason to change. Example: separate InvoiceCalculator from InvoicePrinter instead of one class doing both.
  • Open/Closed Principle (OCP) - Software entities should be open for extension, but closed for modification. Example: add new payment methods by implementing a PaymentProcessor interface rather than editing the checkout service.
  • Liskov Substitution Principle (LSP) - Objects of a superclass must be replaceable with objects of a subclass without altering program correctness. Example: a PremiumCustomer should be usable anywhere a Customer is expected without special-case discount checks.
  • Interface Segregation Principle (ISP) - Clients should not be forced to depend on interfaces they do not use. Example: split a monolithic Machine interface into focused Printer, Scanner, and Fax interfaces so clients only implement what they need.
  • Dependency Inversion Principle (DIP) - High-level modules should depend on abstractions, not concrete implementations. Example: OrderService depends on IOrderRepository so SqlOrderRepository can be swapped with InMemoryOrderRepository for testing.

Examples