Patterns / Structural patterns
image

//Adaptee
class StringList {
    constructor() {
        this.rows = [];
    }

    //SpecificRequest()
    getString() {
        return this.rows.join("\n");
    }

    add(value) {
        this.rows.push(value);
    }
}

//Adapter
class TextAdapter {
    constructor(rowList) {
        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);


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