js设计模式----创建者模式(1)Builder

本文探讨了在JavaScript中使用Builder模式简化复杂类构造的方法,通过封装构造知识,减少构造函数的复杂性。当需要新的构建逻辑时,只需插入新的Builder,而无需修改构造函数。Builder模式在锦标赛等复杂场景中的应用,通过集中构建逻辑避免了分散初始化代码的问题,允许不同具体Builder创建不同的复杂对象。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值