Patterns / Structural patterns
image

//Target
interface IText {
    //Request()
    fun getText(): String
}

//Adaptee
open class StringList {
    private val rows = mutableListOf<String>()

    //SpecificRequest()
    fun getString(): String {
        return rows.joinToString("\n")
    }

    fun add(value: String) {
        rows.add(value)
    }
}

//Adapter
class TextAdapterIText {
    var rowList: StringList? = null

    //Request()
    override fun getText(): String {
        if (rowList == null) {
            return ""
        }
        return rowList!!.getString()
    }
}

private fun getTextAdapter(): TextAdapter {
    val adapter = TextAdapter()
    val rowList = StringList()
    rowList.add("line 1")
    rowList.add("line 2")
    adapter.rowList = rowList
    return adapter
}

//Client
val adapter = getTextAdapter()
val text = adapter.getText()
//text: line 1
//      line 2

println("text is $text")


Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.