当前位置: 移动技术网 > IT编程>开发语言>JavaScript > javascript设计模式 – 建造者模式原理与应用实例分析

javascript设计模式 – 建造者模式原理与应用实例分析

2020年05月12日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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代码运行工具:测试上述代码运行效果。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网