Реализации на Kotlin
Adapter
Общая реализация на языке Kotlin
interface IThermometer {
val temperature: Double
}class CelsiusThermometer : IThermometer {
override val temperature: Double = DEFAULT_TEMPERATURE
companion object {
private const val DEFAULT_TEMPERATURE: Double = 15.0
}
}interface ITempAdapter {
val temperature: Double
}class FahrenheitTempAdapter(
private val thermometer: IThermometer
) : ITempAdapter {
override val temperature: Double = thermometer.temperature * FACTOR + LINEAR_COEFFICIENT
companion object {
private const val FACTOR = 1.8
private const val LINEAR_COEFFICIENT = 32.0
}
}fun main() {
val thermometer: IThermometer = CelsiusThermometer()
println("Temperature in celsius: ${thermometer.temperature}")
val adapter: ITempAdapter = FahrenheitTempAdapter(thermometer)
println("Temperature in fahrenheit: ${adapter.temperature}")
}Last updated
Was this helpful?