Different adapters

So, we need two types of adapters.

In Java, you would usually create a pair of classes for that purpose. In Kotlin, we can replace those with extension functions.

We could adopt the US plug to work with the EU plug by using the following extension function:

fun USPlug.toEUPlug() : EUPlug {
return object : EUPlug {
// Do something to convert
}
}

We can create a USB Adapter between mini USB and type-C USB in a similar way:

fun UsbMini.toUsbTypeC() : UsbTypeC {
return object : UsbTypeC {
// Do something to convert
}
}

And finally, we get back online by combining all those adapters together:

cellPhone(
charger(
powerOutlet().toEUPlug()
).toUsbTypeC()
)

As you can see, we don't need to compose one object inside the other to adapt them. Nor, luckily, do we need to inherit both interface and implementation. With Kotlin, our code stays short and to the point.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.55.18