liveBook · Manning

SOLID principles

As explained in Functional Design and Architecture: Examples in Haskell

SOLID is a set of five object-oriented design principles concerning responsibility, extensibility, substitutability, interface design, and dependency management.

SOLID is a set of design rules for making code easier to understand, maintain, reuse, and reason about. Although associated with object-oriented programming, these principles also apply wherever interfaces and implementations meet, including functional design. section 1.4.3 section 1.6

The acronym names five complementary principles:

  • SRP — Single Responsibility Principle: give each part of the code one responsibility. section 1.4.3
  • OCP — Open/Closed Principle. section 1.4.3
  • LSP — Liskov Substitution Principle: implementations must preserve the behavior promised by their interfaces. section 1.4.3
  • ISP — Interface Segregation Principle. section 1.4.3
  • DIP — Dependency Inversion Principle. section 1.4.3

A substitution failure

An ILampSwitcher interface promises switch behavior. DaylightLamp satisfies that contract, but TableLamp throws an unexpected exception after switching. Code that accepts a list of ILampSwitcher objects and turns them all off therefore cannot substitute TableLamp transparently, violating LSP. section 1.4.3

trait ILampSwitcher {                              #1
  def switch(onOff: bool)
}
  
class DaylightLamp extends ILampSwitcher           #2
class TableLamp    extends ILampSwitcher           #3
  
def turnAllOff(lamps: List[ILampSwitcher]) {       #4
  lamps.foreach(_.switch(false))
}

The interface and turnAllOff example makes transparent substitution concrete before inspecting the faulty implementation. section 1.4.3

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Defines all five principles and works through LSP using a contract-violating lamp implementation.
Examples
Uses ILampSwitcher, DaylightLamp, TableLamp, and turnAllOff to demonstrate substitution and contract violation.
What it assumes
Assumes interfaces and implementations as the basic vocabulary for discussing design contracts.
What you can do after
Evaluate design decisions with SOLID principles across object-oriented and functional code.
How it connects
Connects SOLID to inversion of control, interfaces, modularity, and functional programming.

Also explained in

Compare how 3 books explain SOLID principles →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.