网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何使用javascript中的建造者模式

这篇文章主要讲解了如何使用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 = '
notice
'; } 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 = '
toast
'; } 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
notice
toastIns.show(); //提示: toast
toast

在建造者模式中,客户端需要通过指挥类(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 = '
notice
'; } 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 = '
toast
'; } 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
notice
toastIns.show(); //提示: toast
toast

建造者模式总结:

优点:
* 建造者模式中,客户端不需要知道产品内部组成的细节,将产品使用与其创建解耦,使得相同创建过程可以创建不同的产品对象
* 每个具体的建造类都相对独立,方便替换和新增,满足开关原则

缺点:
* 建造者模式需要多个产品存在相同的创建流程,如果产品差异性大,就不适用建造者模式。
* 如果产品内部结构复杂多变,就需要定义很多建造类来实现这种变化,会导致系统变得庞大

看完上述内容,是不是对如何使用javascript中的建造者模式有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。


当前名称:如何使用javascript中的建造者模式
转载来源:http://bjjierui.cn/article/ihgdih.html

其他资讯