liveBook · Manning

Object-oriented programming

As explained in Functional Design and Architecture: Examples in Haskell

A programming paradigm that organizes software around objects combining state with behavior and supports mechanisms such as encapsulation, inheritance, and polymorphism.

A control system may need to call device APIs with inconsistent names and interfaces. That mismatch is not an essential feature of the system: it is complexity the software design can manage. Object-oriented programming organizes the implementation around classes and objects; object-oriented design uses their interfaces and patterns to divide responsibilities and limit the complexity added by the code itself. section 1.3 section 1.4.2

An adapter addresses one concrete mismatch. A thermometer reports Kelvin, while a sensor interface expects Celsius. Rather than changing every sensor client, an adapter wraps the thermometer, converts its reading, and exposes the expected interface. The client can then depend on the sensor interface instead of the device-specific API. section 1.4.2

Listing 1.5 Adapter pattern
final class HighAccuracyThermometer {                    #1
  def name() : String = "HAT-53-2"
  def getKelvin() : Float = {
      native.core.highAccuracyThermometer.getData()
  }
}
  
final class HAThermometerAdapter (                       #2
    thermometer: HighAccuracyThermometer) 
  extends ISensor {
    val t = thermometer                                  #3
    
    def getData() : Float = {                            #4
        val data = t.getKelvin()
        native.core.utils.toCelsius(data)
    }
    def getName() : String = t.name()
    def getDataType() : String = "temperature"
}

The listing shows the thermometer wrapper, its sensor interface, and the conversion at the interface boundary. section 1.4.2

Interfaces also create promises. If code switches off a collection of lamps through one interface, each implementation must behave as that interface leads callers to expect. A lamp that switches off but then throws an unexpected exception breaks that promise. The Liskov substitution principle requires a concrete implementation to be usable without forcing client code to account for its identity. section 1.4.3

Listing 1.6 Concrete lamp code
class DaylightLamp (n: String, v: Int, onOff: Boolean)  #1
  extends ILampSwitcher {
    var isOn: Boolean = onOff
    var value: Int    = v
    val name: String  = n
    def switch(onOff: Boolean) = { 
      isOn = onOff
    }
}
  
class TableLamp (n: String, onOff: Boolean)             #2
extends ILampSwitcher {
  var isOn: Boolean = onOff
  val name: String  = n
  def switch(onOff: Boolean) = {  
    isOn = onOff
    // Debug: will remove it later!
    throw new Exception("switched")                     #3
  }
}

The two lamp implementations make the contract failure visible: one switches normally, while the other throws after switching. section 1.4.3

SOLID principles offer rules for reasoning about maintainable object-oriented code, including responsibilities, interfaces, and substitution. They are not a reason to add classes indiscriminately. Some structures built with object-oriented patterns, such as command or strategy, can instead be expressed with higher-order functions, lambdas, and closures. Choose the structure that keeps responsibilities clear and complexity manageable. section 1.4.3 section 1.4.2

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Defines object-oriented design, works through an adapter, and examines an interface-contract failure without teaching object-oriented language syntax from zero.
Examples
Uses a Kelvin-to-Celsius thermometer adapter and two lamp implementations, one of which throws an unexpected exception.
What it assumes
Assumes some familiarity with classes and interfaces while explaining their design roles rather than introducing their syntax from zero.
What you can do after
Evaluate/understand object-oriented designs by checking interface fit, substitution contracts, and whether a pattern reduces complexity.
How it connects
Connects interfaces and design patterns to SOLID, Liskov substitution, UML, and functional programming alternatives.

Also explained in

Compare how 9 books explain object-oriented programming →

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.