模式 / 之前的版本 / 创建型模式
image

//ConcreteBuilder 1
function TextBuilder() {
    var text = "";

    this.addText = function(value) {
        text += value;
    }

    this.addNewLine = function(value) {
        text += "\n" + value;
    }

    this.getText = function() {
        return text;
    }
}

//ConcreteBuilder 2
function HtmlBuilder() {
    var html = "";

    this.addText = function(value) {
        html += "<span>" + value + "</span>";
    }

    this.addNewLine = function(value) {
        html += "\n";
        this.addText(value);
    }

    this.getHtml = function() {
        return html;
    }
}

//Director
function TextMaker() {
    this.makeText = function(textBuilder) {            
        textBuilder.addText("line 1");
        textBuilder.addNewLine("line 2");
    }
}

//Client
var textMaker = new TextMaker();

var textBuilder = new TextBuilder();
textMaker.makeText(textBuilder);
var text = textBuilder.getText();
//text: line 1
//      line 2

var htmlBuilder = new HtmlBuilder();
textMaker.makeText(htmlBuilder);
var html = htmlBuilder.getHtml();
//html: <span>line 1</span><br/>
//      <span>line 2</span>

console.log(`text:\n${text}`);
console.log(`html:\n${html}`);