liveBook · Manning

Design pattern

As explained in Functional Design and Architecture: Examples in Haskell

An object-oriented pattern is a language-agnostic, reusable solution structure for a recurring object-oriented design problem.

A design pattern is a reusable, language-agnostic structure for solving a recurring design problem. Object-oriented patterns organize classes, interfaces, inheritance, encapsulation, and mutable interactions between objects. section 1.4.2 section 1.5.3

Adapting an incompatible thermometer

A high-accuracy thermometer returns Kelvin data but cannot implement an ISensor interface expecting Celsius. HAThermometerAdapter wraps it, converts the unit, and exposes the required sensor operations. section 1.4.2

Patterns describe how a system should work, while idioms describe an object's internal properties and transformations. Functional techniques can sometimes replace class-heavy patterns: higher-order functions, lambdas, and closures may provide the behavior of command or strategy more concisely. section 1.5.3 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"
}

Inspect the adapter implementation, including the wrapped thermometer, Celsius conversion, and ISensor-compatible methods. section 1.4.2

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Defines patterns, distinguishes them from idioms, and works through an Adapter implementation while stopping short of a complete catalog.
Examples
Demonstrates an Adapter that converts Kelvin thermometer data into Celsius through an ISensor-compatible interface.
What you can do after
Apply it by translating a language-agnostic pattern into an object-oriented language and evaluating functional alternatives.
How it connects
Connects object-oriented design with idioms, functional programming, higher-order functions, lambdas, closures, and monads.

Also explained in

Compare how 7 books explain design pattern →

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.