如何运用JavaScript实现建造者模式及其应用案例?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1538个文字,预计阅读时间需要7分钟。
本文实例讲述了JavaScript设计模式——建造者模式原理与应用。分享给广大读者,供大家参考。
介绍:建造者模式,又称生成器模式,是一种设计模式,用于创建复杂对象。它将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
建造者模式原理与应用:
1.原理:
建造者模式通过将一个复杂对象的构建过程分解成多个步骤,使得每个步骤只负责构建对象的一部分,从而简化了对象的创建过程。2. 应用:建造者模式适用于以下场景:- 创建复杂对象,且对象的构建过程涉及多个步骤。- 构建的对象内部结构复杂,难以直接使用new操作符创建。- 构建过程需要灵活调整,便于扩展。
具体如下:- 介绍:建造者模式又称生成器模式,是一种设计模式,用于创建复杂对象。它是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
- 建造者模式原理:建造者模式将一个复杂对象的构建过程分解成多个步骤,每个步骤只负责构建对象的一部分。通过定义一个抽象的建造者接口,实现具体的建造者类,将构建过程抽象化。
- 应用:以下是一个简单的建造者模式示例:
javascript// 抽象建造者class Builder { constructor() { this.product=new Product(); }
buildPartA() { this.product.addPartA(); }
buildPartB() { this.product.addPartB(); }
getBuildResult() { return this.product; }}
// 具体建造者class ConcreteBuilder extends Builder { buildPartA() { this.product.partA='partA'; }
buildPartB() { this.product.partB='partB'; }}
// 产品class Product { constructor() { this.partA=''; this.partB=''; }
addPartA() { this.partA='partA'; }
addPartB() { this.partB='partB'; }}
// 指挥者class Director { constructor(builder) { this.builder=builder; }
construct() { this.builder.buildPartA(); this.builder.buildPartB(); }}
// 使用const builder=new ConcreteBuilder();const director=new Director(builder);director.construct();const product=builder.getBuildResult();console.log(product); // { partA: 'partA', partB: 'partB' }
在这个示例中,通过建造者模式,我们可以灵活地创建具有不同内部结构的Product对象。
本文实例讲述了javascript设计模式 – 建造者模式原理与应用。分享给大家供大家参考,具体如下:
介绍:建造者模式又称为生成器模式,它是一种较为复杂、使用频率相对较低的创建型模式。建造者模式为客户端返回的不是一个简单的产品,而是一个由多个部件组成的复杂产品
定义:将一个复杂对象的构建与他的表示分离,使得同样的构建过程可以创建不同的表示。建造者模式是一种对象创建型模式。
示例:
var Dialog = function (){ this.type = ''; this.name = ''; this.element = ''; this.show = function(){ console.log(this.name + ': ' + this.type + this.element); } } var noticeBuilder = function(){ this.dialog = new Dialog(); this.setType = function(){ this.dialog.type = 'notice'; } this.setName = function(){ this.dialog.name = '公告'; } this.setElement = function(){ this.dialog.element = '<div>notice</div>'; } this.getDialog = function(){ return this.dialog; } } var toastBuilder = function(){ this.dialog = new Dialog(); this.setType = function(){ this.dialog.type = 'toast'; } this.setName = function(){ this.dialog.name = '提示'; } this.setElement = function(){ this.dialog.element = '<div>toast</div>'; } this.getDialog = function(){ return this.dialog; } } function construct(ab){ ab.setType(); ab.setName(); ab.setElement(); return ab.getDialog(); } var notice = new noticeBuilder(); var toast = new toastBuilder(); var noticeIns = construct(notice); var toastIns = construct(toast); noticeIns.show(); //公告: notice<div>notice</div> toastIns.show(); //提示: toast<div>toast</div>
在建造者模式中,客户端需要通过指挥类(construct方法)一步一步建造一个完整的产品,相同的构造过程可以创建完全不同的产品。
建造者模式可以将复杂对象的构建与其表示相分离,使用相同构建过程可以创建不同的表示层,用户只需要指定需要建造的类型就可以,而具体的建造过程和细节就不需要知道了。
为了简化系统结构,去掉construct参数,可以将construct合并到builder:
var Dialog = function (){ this.type = ''; this.name = ''; this.element = ''; this.show = function(){ console.log(this.name + ': ' + this.type + this.element); } } var Construct = function(){ this.construct = function(){ this.setType(); this.setName(); this.setElement(); return this.getDialog(); } } var noticeBuilder = function(){ this.dialog = new Dialog(); this.setType = function(){ this.dialog.type = 'notice'; } this.setName = function(){ this.dialog.name = '公告'; } this.setElement = function(){ this.dialog.element = '<div>notice</div>'; } this.getDialog = function(){ return this.dialog; } } var toastBuilder = function(){ this.dialog = new Dialog(); this.setType = function(){ this.dialog.type = 'toast'; } this.setName = function(){ this.dialog.name = '提示'; } this.setElement = function(){ this.dialog.element = '<div>toast</div>'; } this.getDialog = function(){ return this.dialog; } } noticeBuilder.prototype = new Construct(); toastBuilder.prototype = new Construct(); var notice = new noticeBuilder(); var toast = new toastBuilder(); var noticeIns = notice.construct(); var toastIns = toast.construct(); noticeIns.show(); //公告: notice<div>notice</div> toastIns.show(); //提示: toast<div>toast</div>
建造者模式总结:
优点:
* 建造者模式中,客户端不需要知道产品内部组成的细节,将产品使用与其创建解耦,使得相同创建过程可以创建不同的产品对象
* 每个具体的建造类都相对独立,方便替换和新增,满足开关原则
缺点:
* 建造者模式需要多个产品存在相同的创建流程,如果产品差异性大,就不适用建造者模式。
* 如果产品内部结构复杂多变,就需要定义很多建造类来实现这种变化,会导致系统变得庞大
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:tools.jb51.net/code/HtmlJsRun测试上述代码运行效果。
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
本文共计1538个文字,预计阅读时间需要7分钟。
本文实例讲述了JavaScript设计模式——建造者模式原理与应用。分享给广大读者,供大家参考。
介绍:建造者模式,又称生成器模式,是一种设计模式,用于创建复杂对象。它将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
建造者模式原理与应用:
1.原理:
建造者模式通过将一个复杂对象的构建过程分解成多个步骤,使得每个步骤只负责构建对象的一部分,从而简化了对象的创建过程。2. 应用:建造者模式适用于以下场景:- 创建复杂对象,且对象的构建过程涉及多个步骤。- 构建的对象内部结构复杂,难以直接使用new操作符创建。- 构建过程需要灵活调整,便于扩展。
具体如下:- 介绍:建造者模式又称生成器模式,是一种设计模式,用于创建复杂对象。它是将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
- 建造者模式原理:建造者模式将一个复杂对象的构建过程分解成多个步骤,每个步骤只负责构建对象的一部分。通过定义一个抽象的建造者接口,实现具体的建造者类,将构建过程抽象化。
- 应用:以下是一个简单的建造者模式示例:
javascript// 抽象建造者class Builder { constructor() { this.product=new Product(); }
buildPartA() { this.product.addPartA(); }
buildPartB() { this.product.addPartB(); }
getBuildResult() { return this.product; }}
// 具体建造者class ConcreteBuilder extends Builder { buildPartA() { this.product.partA='partA'; }
buildPartB() { this.product.partB='partB'; }}
// 产品class Product { constructor() { this.partA=''; this.partB=''; }
addPartA() { this.partA='partA'; }
addPartB() { this.partB='partB'; }}
// 指挥者class Director { constructor(builder) { this.builder=builder; }
construct() { this.builder.buildPartA(); this.builder.buildPartB(); }}
// 使用const builder=new ConcreteBuilder();const director=new Director(builder);director.construct();const product=builder.getBuildResult();console.log(product); // { partA: 'partA', partB: 'partB' }
在这个示例中,通过建造者模式,我们可以灵活地创建具有不同内部结构的Product对象。
本文实例讲述了javascript设计模式 – 建造者模式原理与应用。分享给大家供大家参考,具体如下:
介绍:建造者模式又称为生成器模式,它是一种较为复杂、使用频率相对较低的创建型模式。建造者模式为客户端返回的不是一个简单的产品,而是一个由多个部件组成的复杂产品
定义:将一个复杂对象的构建与他的表示分离,使得同样的构建过程可以创建不同的表示。建造者模式是一种对象创建型模式。
示例:
var Dialog = function (){ this.type = ''; this.name = ''; this.element = ''; this.show = function(){ console.log(this.name + ': ' + this.type + this.element); } } var noticeBuilder = function(){ this.dialog = new Dialog(); this.setType = function(){ this.dialog.type = 'notice'; } this.setName = function(){ this.dialog.name = '公告'; } this.setElement = function(){ this.dialog.element = '<div>notice</div>'; } this.getDialog = function(){ return this.dialog; } } var toastBuilder = function(){ this.dialog = new Dialog(); this.setType = function(){ this.dialog.type = 'toast'; } this.setName = function(){ this.dialog.name = '提示'; } this.setElement = function(){ this.dialog.element = '<div>toast</div>'; } this.getDialog = function(){ return this.dialog; } } function construct(ab){ ab.setType(); ab.setName(); ab.setElement(); return ab.getDialog(); } var notice = new noticeBuilder(); var toast = new toastBuilder(); var noticeIns = construct(notice); var toastIns = construct(toast); noticeIns.show(); //公告: notice<div>notice</div> toastIns.show(); //提示: toast<div>toast</div>
在建造者模式中,客户端需要通过指挥类(construct方法)一步一步建造一个完整的产品,相同的构造过程可以创建完全不同的产品。
建造者模式可以将复杂对象的构建与其表示相分离,使用相同构建过程可以创建不同的表示层,用户只需要指定需要建造的类型就可以,而具体的建造过程和细节就不需要知道了。
为了简化系统结构,去掉construct参数,可以将construct合并到builder:
var Dialog = function (){ this.type = ''; this.name = ''; this.element = ''; this.show = function(){ console.log(this.name + ': ' + this.type + this.element); } } var Construct = function(){ this.construct = function(){ this.setType(); this.setName(); this.setElement(); return this.getDialog(); } } var noticeBuilder = function(){ this.dialog = new Dialog(); this.setType = function(){ this.dialog.type = 'notice'; } this.setName = function(){ this.dialog.name = '公告'; } this.setElement = function(){ this.dialog.element = '<div>notice</div>'; } this.getDialog = function(){ return this.dialog; } } var toastBuilder = function(){ this.dialog = new Dialog(); this.setType = function(){ this.dialog.type = 'toast'; } this.setName = function(){ this.dialog.name = '提示'; } this.setElement = function(){ this.dialog.element = '<div>toast</div>'; } this.getDialog = function(){ return this.dialog; } } noticeBuilder.prototype = new Construct(); toastBuilder.prototype = new Construct(); var notice = new noticeBuilder(); var toast = new toastBuilder(); var noticeIns = notice.construct(); var toastIns = toast.construct(); noticeIns.show(); //公告: notice<div>notice</div> toastIns.show(); //提示: toast<div>toast</div>
建造者模式总结:
优点:
* 建造者模式中,客户端不需要知道产品内部组成的细节,将产品使用与其创建解耦,使得相同创建过程可以创建不同的产品对象
* 每个具体的建造类都相对独立,方便替换和新增,满足开关原则
缺点:
* 建造者模式需要多个产品存在相同的创建流程,如果产品差异性大,就不适用建造者模式。
* 如果产品内部结构复杂多变,就需要定义很多建造类来实现这种变化,会导致系统变得庞大
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:tools.jb51.net/code/HtmlJsRun测试上述代码运行效果。
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。

