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








