Adapter Pattern in Kotlin

Adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces.

interface Temperature { var temperature: Double } class CelsiusTemperature(override var temperature: Double) : Temperature class FahrenheitTemperature(var celsiusTemperature: CelsiusTemperature) : Temperature { override var temperature: Double get() = convertCelsiusToFahrenheit(celsiusTemperature.temperature) set(temperatureInF) { celsiusTemperature.temperature = convertFahrenheitToCelsius(temperatureInF) } private fun convertFahrenheitToCelsius(f: Double): Double = (f - 32) * 5 / 9 private fun convertCelsiusToFahrenheit(c: Double): Double = (c * 9 / 5) + 32 } fun main(args: Array<String>) { val celsiusTemperature = CelsiusTemperature(0.0) val fahrenheitTemperature = FahrenheitTemperature(celsiusTemperature) celsiusTemperature.temperature = 36.6 println("${celsiusTemperature.temperature} C -> ${fahrenheitTemperature.temperature} F") fahrenheitTemperature.temperature = 100.0 println("${fahrenheitTemperature.temperature} F -> ${celsiusTemperature.temperature} C") }
aaaaaaaaaaaaa
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech