SOLID Principles – a good approach

WINW > Software Architecture > Software Quality > SOLID Principles – a good approach

💡 The SOLID Principles Explained

The acronym stands for the following principles:

  • SSingle Responsibility Principle (SRP)
    • A class should have only one reason to change.
    • In practice, this means a class (or module/function) should have only one job or responsibility. If a class handles multiple, unrelated concerns (like calculating payroll and managing database connections), a change to one concern might accidentally break the other. By separating responsibilities, your code becomes easier to understand, test, and maintain.
  • OOpen-Closed Principle (OCP)
    • Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
    • This means you should be able to add new functionality (extension) without changing the existing, working code (modification). This is typically achieved by using abstraction (interfaces or abstract classes) and polymorphism, allowing new behavior to be introduced via new implementations rather than altering the core class.
  • LLiskov Substitution Principle (LSP)
    • Objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program.
    • Simply put, if you have a class hierarchy, a derived class must adhere to the contract defined by its base class. Subclasses should extend the behavior of the parent class without violating its expected behavior. If substituting a subclass breaks the client code, the LSP is violated.
  • IInterface Segregation Principle (ISP)
    • Clients should not be forced to depend on interfaces they do not use.
    • It is better to have many small, client-specific interfaces (fine-grained) than one large, general-purpose interface (fat interface). This prevents classes from being obligated to implement methods that are irrelevant to their role, thus reducing coupling and improving maintainability.
  • DDependency Inversion Principle (DIP)
    • High-level modules should not depend on low-level modules. Both should depend on abstractions. Additionally, abstractions should not depend on details; details should depend on abstractions.
    • High-level modules contain complex business logic, and low-level modules contain implementation details (like database access or file handling). The DIP advocates for depending on interfaces or abstract classes (abstractions) rather than concrete implementations (details). This decoupling makes the system more flexible and testable (often achieved through Dependency Injection).

Following these principles helps reduce technical debt and allows for easier adaptation to changing requirements over time.

Leave a Reply