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






