面试官:说说你对组合模式的理解?应用场景?

开发 前端
组合模式,又叫 “部分整体” 模式,将对象组合成树形结构,以表示 “部分-整体” 的层次结构。通过对象的多态性表现,使得用户对单个对象和组合对象的使用具有一致性。

一、是什么

组合模式,又叫 “部分整体” 模式,将对象组合成树形结构,以表示 “部分-整体” 的层次结构。通过对象的多态性表现,使得用户对单个对象和组合对象的使用具有一致性

如下面的代码:

var closeDoorCommand = { 
  executefunction () { 
    console.log('关门'); 
  } 
}; 
var openPcCommand = { 
  executefunction () { 
    console.log('开电脑'); 
  } 
}; 
var openQQCommand = { 
  executefunction () { 
    console.log('登录 QQ'); 
  } 
}; 
 
var MacroCommand = function () { 
  return { 
    commandsList: [], 
    addfunction (command) { 
      this.commandsList.push(command); 
    }, 
    executefunction () { 
      for (var i = 0, command; command = this.commandsList[i++];) { 
        command.execute(); 
      } 
    } 
  } 
}; 
 
var macroCommand = MacroCommand(); 
macroCommand.add(closeDoorCommand); 
macroCommand.add(openPcCommand); 
macroCommand.add(openQQCommand); 
macroCommand.execute(); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

上述是命令模式的一个应用,macroCommand命令叫做组合对象,其包含了closeDoorCommand、openPcCommand、openQQCommand三个叶对象

macroCommand 的 execute 方法里,并不执行真正的操作,而是遍历它所包含的叶对象,把真正的 execute 请求委托给这些叶对象

二、应用场景

组合模式应树形结构而生,所以组合模式的使用场景就是出现树形结构的地方:

  • 「命令分发:」 只需要通过请求树的最顶层对象,便能对整棵树做统一的操作。在组合模式中增加和删除树的节点非常方便,并且符合开放-封闭原则;
  • 「统一处理:」 统一对待树中的所有对象,忽略组合对象和叶对象的区别

如将上述例子稍复杂,当我们点击按钮时,出发一系列操作(打开空调,打开电视,打开音响)其中打开电视和打开音响是一组组合对象,如下代码:

<button id=button>按我</button> 
<script> 
  var MacroCommand = function () { 
    return { 
      commandsList: [], 
      addfunction (command) { 
        this.commandsList.push(command); 
      }, 
      executefunction () { 
        for (var i = 0, command; command = this.commandsList[i++];) { 
          command.execute(); 
        } 
      } 
    } 
  }; 
 
  var openAcCommend = { 
    executefunction () { 
      console.log('打开空调'); 
    } 
  } 
 
  // 电视和音响一起打开 
  var openTvCommand = { 
    executefunction () { 
      console.log('打开电视'); 
    } 
  } 
  var openSoundCommand = { 
    executefunction () { 
      console.log('打开音响'); 
    } 
  } 
  var macroCommand1 = MacroCommand() 
  macroCommand1.add(openTvCommand) 
  macroCommand1.add(openSoundCommand) 
 
  // 关门、开电脑、登QQ的命令 
  var closeDoorCommand = { 
    executefunction () { 
      console.log('关门'); 
    } 
  }; 
  var openPcCommand = { 
    executefunction () { 
      console.log('开电脑'); 
    } 
  }; 
  var openQQCommand = { 
    executefunction () { 
      console.log('登录 QQ'); 
    } 
  }; 
  var macroCommand2 = MacroCommand(); 
  macroCommand2.add(closeDoorCommand); 
  macroCommand2.add(openPcCommand); 
  macroCommand2.add(openQQCommand); 
 
  // 所有命令组合成一个超级命令 
  var macroCommand = MacroCommand(); 
  macroCommand.add(openAcCommend) 
  macroCommand.add(macroCommand1) 
  macroCommand.add(macroCommand2) 
 
  // 给超级遥控器绑定命令 
  var setCommand = (function (command) { 
    document.getElementById('button').onclick = function () { 
      command.execute() 
    } 
  })(macroCommand) 
</script> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.

 

组合模式的透明性使得发起请求的客户不用去顾忌树中组合对象和叶对象的区别,但它们在本质上是有区别的。

组合对象可以拥有叶子节点,叶对象下面就没有子节点,所以我们可能会有一些误操作,比如试图往叶对象中添加子节点

解决方案就是给叶对象也增加 add 方法,并且在调用这个方法时,抛出一个异常来及时提醒用户,如下:

var MacroCommand = function () { 
  return { 
    commandsList: [], 
    addfunction (command) { 
      this.commandsList.push(command); 
    }, 
    executefunction () { 
      for (var i = 0, command; command = this.commandsList[i++];) { 
        command.execute(); 
      } 
    } 
  } 
}; 
 
var openAcCommend = { 
  executefunction () { 
    console.log('打开空调'); 
  }, 
  addfunction() { 
    throw new Error('叶对象不能添加子节点'
  } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

三、总结

组合模式常使用树形方式创建对象,如下图:

特点如下:

  • 表示 “部分-整体” 的层次结构,生成 "树叶型" 结构
  • 一致操作性,树叶对象对外接口保存一致(操作与数据结构一致)
  • 自上而下的的请求流向,从树对象传递给叶对象
  • 调用顶层对象,会自行遍历其下的叶对象执行

参考文献

https://www.runoob.com/design-pattern/composite-pattern.html

https://segmentfault.com/a/1190000019773556

 

https://juejin.cn/post/6995851145490989070

 

责任编辑:武晓燕 来源: JS每日一题
相关推荐

2021-11-09 08:51:13

模式命令面试

2021-11-05 07:47:56

代理模式对象

2021-11-03 14:10:28

工厂模式场景

2021-11-11 16:37:05

模板模式方法

2021-11-22 23:50:59

责任链模式场景

2021-09-16 07:52:18

算法应用场景

2021-08-16 08:33:26

git

2021-11-04 06:58:32

策略模式面试

2021-09-06 10:51:27

TypeScriptJavaScript

2021-09-28 07:12:09

测试路径

2021-09-29 07:24:20

场景数据

2021-09-10 06:50:03

TypeScript装饰器应用

2021-10-13 18:01:33

快速排序场景

2021-10-08 09:59:32

冒泡排序场景

2021-10-09 10:25:41

排序应用场景

2021-09-08 07:49:34

TypeScript 泛型场景

2021-05-31 10:35:34

TCPWebSocket协议

2021-06-01 08:25:06

Node.jsJavaScript运行

2021-10-11 09:38:41

开源

2021-10-12 07:15:02

归并排序场景
点赞
收藏

51CTO技术栈公众号