
//Adaptee
class StringList {
rows: string[] = []
//SpecificRequest()
getString() {
return this.rows.join("\n")
}
add(value: string) {
this.rows.push(value)
}
}
//Adapter
class TextAdapter {
rowList: StringList
constructor(rowList: StringList) {
this.rowList = rowList
}
//Request()
getText() {
return this.rowList.getString()
}
}
function getTextAdapter() {
let rowList = new StringList()
let adapter = new TextAdapter(rowList)
rowList.add("line 1")
rowList.add("line 2")
return adapter
}
//Client
let adapter = getTextAdapter()
let text = adapter.getText()
//text: line 1
// line 2
console.log(`text:\n${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. |