SOLID Development Priniciples – in Motivational Posters
Derick Bailey put together a set of Motivational Posters to illustrate the SOLID principles. SOLID is a set of principles that help guide OO code towards greater testability. They increase cohesion and reduce dependencies, hence, coupling.
Single Responsibility Principle — A class should have one, and only one, reason to change
Ideally, a class or a function will do only one thing and do it well, in only a few lines.
Recently, I refactored two large functions. One function proxied an HTTP request: it had to selectively copy request headers, construct other headers, copy the request body, make the request, handle exceptions, selectively copy response headers, construct other headers, and copy the request body. The preceding sentence tells you what the resulting functions looked like.
The other function made a series of related database queries, constructed some intermediate data structures, then performed some joins, to build an object graph. Each query got its own function as did each builder of an intermediate data structure. The top-level function became eight simple lines and is now comprehensible.
Open-Closed Principle — You should be able to extend a class’s behavior, without modifying it
The classic formulation of the Open-Closed Principle is:
Software Entities (classes, modules, functions, etc.) should be Open for Extension, but Closed for Modification.
That’s pithy—and confusing. Let me try restating it: don’t change existing code when you want to add new code.
If you’ve designed your system with abstract base classes and interfaces, then it’s generally possible to achieve this. If you have to add new if-clauses or adjust switch statements in existing code, your code is not closed for modification.
Think “pluggable” or the Template Method and Strategy patterns.
Liskov Substitution Principle — Derived classes must be substitutable for their base classes
Interface Segregation Principle — Make fine grained interfaces that are client specific
italic
Dependency Inversion Principle — Depend on abstractions, not on concretions