定义

它属于创建型模式的一种,将一个复杂的对象分解成多个简单的对象来进行构建,将复杂的构建层与表示层分离,使得相同的构建过程可以创建不同的表示的模式便是建造者模式

举个栗子

现在有一个建造汽车的工厂,他们主要目的是生成汽车,汽车的标识都是相同的(大众),但是汽车轮子有不同的型号,汽车的发动机也是有不同的功率的

三个组成

  • 抽象建造者

    Car就是抽象建造者,它具备一些车的基本属性还有方法, 它是抽象的,用户不需要知道它的存在,
    那么Tyre(轮胎),Engine(发动机),分别就是车的具体部件(具体类),

  • 指挥者

    创建的BenChi类,其实就是一个指挥者的角色,负责把对应的部件组装起来,

  • 具体建造者

函数的调用者

抽象建造者

  1. // #建造者模式
  2. // 抽象建造者
  3. var Car = function (param) {
  4. // 速度
  5. this.speed = param && param.speed || '0';
  6. // 重量
  7. this.weight = param && param.weight || '0';
  8. }
  9. Car.prototype = {
  10. // 获取速度
  11. getSpeed: function () {
  12. return this.speed;
  13. },
  14. // 获取重量
  15. getWeight: function () {
  16. return this.weight
  17. }
  18. }
  19. // 轮胎部件类
  20. var Tyre = function (type) {
  21. var that = this;
  22. // 构造器
  23. // 构造函数中通过传入的type类型设置相对应的轮胎尺寸
  24. (function (type,that) {
  25. switch (type) {
  26. case 'small':
  27. that.tyre = '小号轮胎';
  28. that.tyreIntro = '正在使用小号轮胎';
  29. break;
  30. case 'normal':
  31. that.tyre = '中号轮胎';
  32. that.tyreIntro = '正在使用中号轮胎';
  33. break;
  34. case 'big':
  35. that.tyre = '大号轮胎';
  36. that.tyreIntro = '正在使用大号轮胎';
  37. break;
  38. }
  39. })(type,this);
  40. }
  41. Tyre.prototype = {
  42. // 更换轮胎的方法
  43. changeType: function (type) {
  44. that.tyre = type;
  45. that.tyreIntro = '正在使用'+type;
  46. }
  47. }
  48. // 发动机部件类
  49. var Engine = function (type) {
  50. var that = this;
  51. // 构造器
  52. // 构造函数中通过传入的type类型设置相对应的发动机类型
  53. (function (type,that) {
  54. switch (type) {
  55. case 'small':
  56. that.engine = '小号发动机';
  57. that.engineIntro = '正在使用小号发动机';
  58. break;
  59. case 'normal':
  60. that.engine = '中号发动机';
  61. that.engineIntro = '正在使用中号发动机';
  62. break;
  63. case 'big':
  64. that.engine = '大号发动机';
  65. that.engineIntro = '正在使用大号发动机';
  66. break;
  67. }
  68. })(type,this);
  69. }
  70. Engine.prototype = {
  71. // 更换发动机的方法
  72. changeType: function (type) {
  73. that.engine = type;
  74. that.engineIntro = '正在使用'+type;
  75. }
  76. }

指挥者

  1. /**
  2. * 指挥者,建造一个奔驰车的类
  3. * @param {*轮胎类型 small normal big} tyre
  4. * @param {*发动机类型 small normal big} engine
  5. * @param {*车辆基本属性 param.speed:速度 param.weight: 重量} param
  6. */
  7. var BenChi = function (tyre,engine,param) {
  8. // 创建一个车辆缓存对象
  9. var _car = new Car(param);
  10. // 创建车辆的轮胎
  11. _car.tyreInfo = new Tyre(tyre);
  12. // 创建车辆的发动机
  13. _car.engineInfo = new Engine(engine);
  14. // 将创建的车辆对象返回
  15. return _car;
  16. }

具体建造者

  1. // 具体建造者 实例化奔驰车类
  2. var benchi1 = new BenChi('small','big',{speed: 200,weight: '200'});
  3. console.log(benchi1.speed);// 200
  4. console.log(benchi1.weight);// 200
  5. console.log(benchi1.tyreInfo.tyre);// 小号轮胎
  6. console.log(benchi1.tyreInfo.tyreIntro);// 正在使用小号轮胎
  7. console.log(benchi1.engineInfo.engine);// 大号发动机
  8. console.log(benchi1.engineInfo.engineIntro);// 正在使用大号发动机

总结

优点

  • 在建造者模式中,客户端不必知道产品内部组成的细节,将产品本身与产品的创建过程解耦,使得相同的创建过程可以创建不同的产品对象。
  • 每一个具体建造者都相对独立,而与其他的具体建造者无关,因此可以很方便地替换具体建造者或增加新的具体建造者,用户使用不同的具体建造者即可得到不同的产品对象。