js設計模式----創建者模式(1)Builder

In our fictional world we sometimes have some rather complicated classes, which
need to be constructed. The classes contain different implementations of an interface
depending on how they are constructed. In order to simplify the building of these
classes and encapsulate the knowledge about building the class away from the
consumers, a builder may be used. Multiple concrete builders reduce the complexity
of the constructor in the implementation. When new builders are required, a
constructor does not need to be added, a new builder just needs to be plugged in.
在我們虛構的世界裏, 我們有時有一些需要構造的有點複雜的類 。這些類包含接口的不同實現取決於它們是如何構造的。爲了簡化這些類以及封裝這些類, 使用builder。

錦標賽是一個複雜的例子。每個錦標賽都有一個 複雜的設置涉及事件, 與會者和獎品。大部分的這些錦標賽的設置是相似的: 每一個都有一個格鬥, 射箭和混戰。 Creating a tournament from multiple places in the code means that the responsibility for knowing how to construct a tournament is distributed. If there is a need to change the initiation code then it must be done in a lot of different places.
Employing a builder pattern avoids this issue by centralizing the logic necessary to build the object. Different concrete builders can be plugged into the builder to construct different complicated objects. The relationship between the various classes in the builder pattern is shown here
這裏寫圖片描述

let Event = (function () {
    function Event(name) {
        this.name = name;
    }
    return Event;
})();
Westeros.Event = Event;

let Prize = (function () {
    function Prize(name) {
        this.name = name;
    }
    return Prize;
})();
Westeros.Prize = Prize;

let Attendee = (function () {
    function Attendee(name) {
        this.name = name;
    }
    return Attendee;
})();
Westeros.Attendee = Attendee;

let Tournament = (function () {
    this.Events = [];
    function Tournament() {
    }
    return Tournament;
})();
Westeros.Tournament = Tournament;

let LannisterTournamentBuilder = (function () {
    function LannisterTournamentBuidler() {
    }
    LannisterTournamentBuidler.prototype.build = function () {
        var tournament = new Tournament();
        tournament.events.push(new Event("Joust"));
        tournament.events.push(new Event("Melee"));
        tournament.attendees.push(new Attendee("Jamie"));
        tournament.prizes.push(new Prize("Gold"));
        tournament.prizes.push(new Prize("More Gold"));
        return tournament;
    }
    return LannisterTournamentBuidler;
})();
Westeros.LannisterTournamentBuilder = LannisterTournamentBuilder;

let BaratheonTournamentBuilder = (function () {
    function BarathonTournamentBuilder() {
    }
    BarathonTournamentBuilder.prototype.build = function () {
        let tournament = new Tournament();
        tournament.events.push(new Event("Joust"));
        tournament.events.push(new Event("Melee"));
        tournament.attendees.push(new Attendee("Stannis"));
        tournament.attendees.push(new Attendee("Robert"));
        return tournament;
    }
    return BarathonTournamentBuilder;
})()
Westeros.BarathonTournamentBuilder = BaratheonTournamentBuilder;

let TournamentBuidler = (function () {
    function TournamentBuilder() {
    }
    TournamentBuilder.prototype.build = function (builder) {
        return builder.build();
    };
    return TournamentBuilder;
})();
Westeros.TournamentBuilder = TournamentBuidler;

Builders need not return a fully realized object. This means that you can create
a builder which partially hydrates an object then allows the object to be passed
on to another builder for it to finish. A good real world analogy might be the
manufacturing process for a car
建設者不需要返回一個完全實現的對象。這意味着您可以創建對象的一部分然後這個對象被傳遞到另一個建設者來完成它。一個好的現實世界的類比可能是汽車製造過程。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章