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
建设者不需要返回一个完全实现的对象。这意味着您可以创建对象的一部分然后这个对象被传递到另一个建设者来完成它。一个好的现实世界的类比可能是汽车制造过程。