基本概念

将对象组合成树形结构以表示“部分-整体”的层次结构

目的

解耦客户程序与复杂元素内部架构,使得用户对单个对象和组合对象的使用具有一致性

结构

类似DOM机制,一个节点可以包含子节点,不管是父节点还是子节点都有添加、删除、遍历子节点的通用功能。

经典案例

  1. /**
  2. * Folder 可以加 Folder, Folder可以加File
  3. * 通过 file.parent 进行
  4. */
  5. class Folder {
  6. constructor (name) {
  7. this.name = name
  8. this.parent = null
  9. this.files = []
  10. }
  11. add (file) {
  12. file.parent = this
  13. this.files.push(file)
  14. return this
  15. }
  16. scan () {
  17. console.log('start scan folder: ', this.parent && this.parent.name, '->', this.name)
  18. for (let i = 0, file, files = this.files; file = files[i++];) {
  19. file.scan()
  20. }
  21. return this
  22. }
  23. remove () {
  24. // 根节点 | 树外的游离节点
  25. if (!this.parent) return
  26. for (let files = this.parent.files, l = files.length - 1; l >= 0; l--) {
  27. const file = files[l]
  28. if (this === file) {
  29. files.splice(l, 1)
  30. }
  31. }
  32. return this
  33. }
  34. }
  35. class File {
  36. constructor (name) {
  37. this.name = name
  38. this.parent = null
  39. }
  40. add () {
  41. throw new Error('can not add file below')
  42. return this
  43. }
  44. scan () {
  45. console.log('start scan file: ', this.parent && this.parent.name, ' -> ', this.name)
  46. return this
  47. }
  48. remove () {
  49. if (!this.parent) return
  50. for (let files = this.parent.files, l = files.length - 1; l >=0; l--) {
  51. const file = files[l]
  52. if (file === this) {
  53. files.splice(l, 1)
  54. }
  55. }
  56. return this
  57. }
  58. }
  59. const folder = new Folder('学习资料')
  60. const folder1 = new Folder('Javascript')
  61. const file1 = new File('深入浅出 Node.js')
  62. const reactFile = new File('React源码解析')
  63. const reduxFolder = new Folder('redux').add(
  64. new File('react-redux')
  65. )
  66. folder1.add(new File('Javascript 设计模式与开发实战'))
  67. folder1.add(reactFile)
  68. folder1.add(
  69. new Folder('函数式编程').add(
  70. reduxFolder
  71. )
  72. )
  73. folder.add(folder1)
  74. folder.add(file1)
  75. folder.scan()
  76. console.log('*********')
  77. reduxFolder.scan()
  78. reduxFolder.remove()
  79. // folder.scan()

项目情况

image.png

初始代码

  1. /* 添加节点 */
  2. handleNodeAdd = (type, node) => {
  3. const localNode = node;
  4. const {
  5. flow
  6. } = this.state;
  7. const newChildNode = REGISTRY[type].component.getNewNodeConfig();
  8. if (type === REGISTRY.condition.type) {
  9. newChildNode.prevId = localNode.nodeId;
  10. localNode.conditionNodes.push(newChildNode);
  11. } else {
  12. const oldChildNode = localNode.childNode;
  13. if (newChildNode) {
  14. newChildNode.prevId = localNode.nodeId;
  15. }
  16. if (oldChildNode) {
  17. oldChildNode.prevId = newChildNode.nodeId;
  18. }
  19. localNode.childNode = newChildNode;
  20. newChildNode.childNode = oldChildNode;
  21. }
  22. this.setState({
  23. flow: {
  24. ...flow
  25. }
  26. });
  27. };
  1. /* 删除节点 */
  2. handleNodeDelete = (node) => {
  3. const {
  4. childNode,
  5. prevId
  6. } = node;
  7. const {
  8. flow
  9. } = this.state;
  10. const findParent = (id) => {
  11. let parent = null;
  12. const find = (co) => {
  13. if (co.nodeId === id) {
  14. parent = co;
  15. }
  16. if (co.type === REGISTRY.route.type) {
  17. for (let i = 0; i < co.conditionNodes.length; i += 1) {
  18. parent = find(co.conditionNodes[i]);
  19. if (parent !== null) { break; }
  20. }
  21. }
  22. if (parent !== null) {
  23. return parent;
  24. }
  25. if (_.isObject(co.childNode)) {
  26. return find(co.childNode);
  27. }
  28. return null;
  29. };
  30. return find(flow);
  31. };
  32. let parentNode = findParent(prevId);
  33. let oldChildNode = childNode;
  34. let isDelCondition = false;
  35. // 删除条件节点
  36. // 1. 如果条件节点的父级路由节点有大于等于2个的条件,删除该条件,否则删除该路由节点
  37. if (parentNode.type === REGISTRY.route.type && node.type === REGISTRY.condition.type) {
  38. if (parentNode.conditionNodes.length === 2) {
  39. oldChildNode = parentNode.childNode;
  40. parentNode = findParent(parentNode.prevId);
  41. } else {
  42. for (let i = parentNode.conditionNodes.length - 1; i >= 0; i -= 1) {
  43. if (parentNode.conditionNodes[i].nodeId === node.nodeId) {
  44. parentNode.conditionNodes.splice(i, 1);
  45. isDelCondition = true;
  46. break;
  47. }
  48. }
  49. }
  50. }
  51. if (!isDelCondition) {
  52. parentNode.childNode = oldChildNode;
  53. if (oldChildNode) {
  54. oldChildNode.prevId = parentNode.nodeId;
  55. }
  56. }
  57. this.setState({
  58. flow: {
  59. ...flow
  60. }
  61. });
  62. };
  1. /* 编辑节点 */
  2. handleOpenSetting = () => {
  3. if (this.isDelClick) {
  4. this.isDelClick = false;
  5. return;
  6. }
  7. const { data } = this.state;
  8. approverSetting.open({
  9. value: data,
  10. onOk: (result) => {
  11. this.setState({
  12. data: result.value
  13. });
  14. // 不纯洁的代码
  15. _.setWith(this.props.config, 'properties', Approver.propertiesEncode(result.value));
  16. }
  17. });
  18. };

修改后代码

  1. /* 类 */
  2. class Approver {
  3. constructor (prevId) {
  4. if (!prevId) throw new Error('Approver node prevId necessary.');
  5. this.type = REGISTRY.approver.type;
  6. this.name = APPROVAL_TITLE;
  7. this.nodeId = uniqueID();
  8. this.prevId = prevId;
  9. this.properties = {};
  10. this.childNode = undefined;
  11. this.parentNode = undefined;
  12. }
  13. addChildNode = node => {
  14. const childNode = this.childNode;
  15. switch (node.type) {
  16. /* 路由节点 */
  17. case nodeTypes.ROUTE:
  18. this.childNode = node;
  19. node.parentNode = this;
  20. if (childNode) {
  21. this.childNode.childNode = childNode;
  22. childNode.parentNode = node;
  23. }
  24. break;
  25. /* 普通节点 */
  26. default:
  27. this.childNode = node;
  28. node.parentNode = this;
  29. if (childNode) {
  30. this.childNode.childNode = childNode;
  31. childNode.parentNode = node;
  32. }
  33. break;
  34. }
  35. }
  36. delete = () => {
  37. if (this.childNode) {
  38. this.parentNode.childNode = this.childNode;
  39. this.childNode.parentNode = this.parentNode;
  40. } else {
  41. this.parentNode.childNode = undefined;
  42. }
  43. }
  44. update = properties => {
  45. this.properties = properties;
  46. }
  47. submit() {
  48. this.parentNode = null;
  49. this.addChildNode = null;
  50. this.delete = null;
  51. this.update = null;
  52. if (this.childNode && this.childNode.submit) this.childNode.submit();
  53. }
  54. decode = (config) => {
  55. /* ... */
  56. };
  57. encode = (data) => {
  58. /* ... */
  59. }
  60. }
  61. export default Approver;
  1. /* 增删节点 */
  2. handleNodeDelete = (node) => {
  3. const { forceUpdate } = this;
  4. node.delete();
  5. forceUpdate();
  6. };
  7. handleNodeAdd = (type, node) => {
  8. const { forceUpdate } = this;
  9. const newChildNode = new classMap[type](node.nodeId);
  10. node.addChildNode(newChildNode);
  11. forceUpdate();
  12. };