Wednesday, 28 October 2009

SOLID principles

The term SOLID is an acronym for:
  • Single Responsibility Principle
    • A class should have one, and only one, reason to change.
    • If a class assumes more than one responsibility, then there will be more than one reason for it to
    • If a class has more then one responsibility, then the responsibilities become coupled.
  • Open-Closed Principle
    • Software entities should be open for extension but closed for modification.
    • You should be able to extend a classes behaviour, without modifying it.
    • The primary mechanisms behind the Open-Closed principle are abstraction (inheritance) and polymorphism.
  • Liskov Substitution Principle
    • Derived classes must be substitutable for their base classes.
    • Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.
    • A.K.A. Design By Contract.
    • Derived types are completely substitutable for their base types.
  • Interface Segregation Principle
    • Make fine grained interfaces that are client specific.*
    • Clients should not be forced to depend upon interfaces that they do not use.
  • Dependency Inversion Principle
    • Depend on abstractions, not on concretions.
    • Modules that encapsulate high level policy should not depend upon
      modules that implement details.
    • High level modules should not depend upon low level modules. Both should depend upon abstractions.
    • Abstractions should not depend upon details. Details should depend upon abstractions.

* “This principle deals with the disadvantages of “fat” interfaces. Classes that have “fat” interfaces are classes whose interfaces are not cohesive. In other words, the interfaces of the class can be broken up into groups of member functions. Each group serves a different set of clients. Thus some clients use one group of member functions, and other clients use the other groups.”