SOLID principles
SOLID is an acronym for five design principles of object-oriented programming. The five principles are:
- the single responsibility principle;
- the open-closed principle;
- the Liskov substitution principle;
- the interface segregation principle;
- and the dependency inversion.
Robert C. Martin introduced the SOLID acronym in his 2000 essay Design Principles and Design Patterns, gathering principles he had been writing about through the 1990s into a single mnemonic. Two of the five principles predate the essay: the Liskov substitution principle was stated by Barbara Liskov in 1987, and the open-closed principle by Bertrand Meyer in 1988. The other three emerged from Martin’s own series of C++ Report columns in 1996.
Martin framed the principles as remedies for the symptoms of poor design he called software rot:
- rigidity, where every change forces many others;
- fragility, where a change breaks unrelated parts;
- immobility, where a module cannot be reused because it drags in too many dependencies;
- and viscosity, where it is easier to hack around the design than to follow it.
The five principles form a set of design preferences – for high cohesion, low coupling, and dependence on abstractions rather than concretes – that, taken together, push a codebase away from those symptoms.
The five principles are interdependent but build on one another. The single responsibility principle forms the foundation. A module with one reason to change is easier to extend without modifying, which is the goal of the open-closed principle. The open-closed principle is in turn easiest to satisfy when subtypes are genuine behavioral substitutes (the Liskov substitution principle), when interfaces are narrow enough that every implementer can honor the full contract (the interface segregation principle), and when callers depend on abstractions owned by the caller rather than on concrete implementations (dependency inversion).
The principles operate at the scale of a single class or module and its immediate collaborators. They are not architectural principles.
SOLID overlaps with GRASP, Craig Larman’s set of responsibility-assignment patterns, which express similar preferences for low coupling and high cohesion but frame them as questions about which class should own a responsibility. SOLID is also a specific application of separation of concerns at the class and module level.
The Law of Demeter is another complementary heuristic. It constrains how an object talks to its collaborators, where the SOLID letters constrain the shape of the modules that collaborate.
See also
References
- Martin, Robert C. (2000). Design Principles and Design Patterns. objectmentor.com.
- Martin, Robert C. (2002). Agile Software Development, Principles, Patterns, and Practices. Pearson.
- Liskov, Barbara (1987). Data Abstraction and Hierarchy in Object-Oriented Programming. OOPSLA.
- Meyer, Bertrand (1988). Object-Oriented Software Construction. Prentice Hall.