基本概念
目的
解耦客户程序与复杂元素内部架构,使得用户对单个对象和组合对象的使用具有一致性
结构
类似DOM机制,一个节点可以包含子节点,不管是父节点还是子节点都有添加、删除、遍历子节点的通用功能。
经典案例
/*** Folder 可以加 Folder, Folder可以加File* 通过 file.parent 进行*/class Folder {constructor (name) {this.name = namethis.parent = nullthis.files = []}add (file) {file.parent = thisthis.files.push(file)return this}scan () {console.log('start scan folder: ', this.parent && this.parent.name, '->', this.name)for (let i = 0, file, files = this.files; file = files[i++];) {file.scan()}return this}remove () {// 根节点 | 树外的游离节点if (!this.parent) returnfor (let files = this.parent.files, l = files.length - 1; l >= 0; l--) {const file = files[l]if (this === file) {files.splice(l, 1)}}return this}}class File {constructor (name) {this.name = namethis.parent = null}add () {throw new Error('can not add file below')return this}scan () {console.log('start scan file: ', this.parent && this.parent.name, ' -> ', this.name)return this}remove () {if (!this.parent) returnfor (let files = this.parent.files, l = files.length - 1; l >=0; l--) {const file = files[l]if (file === this) {files.splice(l, 1)}}return this}}const folder = new Folder('学习资料')const folder1 = new Folder('Javascript')const file1 = new File('深入浅出 Node.js')const reactFile = new File('React源码解析')const reduxFolder = new Folder('redux').add(new File('react-redux'))folder1.add(new File('Javascript 设计模式与开发实战'))folder1.add(reactFile)folder1.add(new Folder('函数式编程').add(reduxFolder))folder.add(folder1)folder.add(file1)folder.scan()console.log('*********')reduxFolder.scan()reduxFolder.remove()// folder.scan()
项目情况

初始代码
/* 添加节点 */handleNodeAdd = (type, node) => {const localNode = node;const {flow} = this.state;const newChildNode = REGISTRY[type].component.getNewNodeConfig();if (type === REGISTRY.condition.type) {newChildNode.prevId = localNode.nodeId;localNode.conditionNodes.push(newChildNode);} else {const oldChildNode = localNode.childNode;if (newChildNode) {newChildNode.prevId = localNode.nodeId;}if (oldChildNode) {oldChildNode.prevId = newChildNode.nodeId;}localNode.childNode = newChildNode;newChildNode.childNode = oldChildNode;}this.setState({flow: {...flow}});};
/* 删除节点 */handleNodeDelete = (node) => {const {childNode,prevId} = node;const {flow} = this.state;const findParent = (id) => {let parent = null;const find = (co) => {if (co.nodeId === id) {parent = co;}if (co.type === REGISTRY.route.type) {for (let i = 0; i < co.conditionNodes.length; i += 1) {parent = find(co.conditionNodes[i]);if (parent !== null) { break; }}}if (parent !== null) {return parent;}if (_.isObject(co.childNode)) {return find(co.childNode);}return null;};return find(flow);};let parentNode = findParent(prevId);let oldChildNode = childNode;let isDelCondition = false;// 删除条件节点// 1. 如果条件节点的父级路由节点有大于等于2个的条件,删除该条件,否则删除该路由节点if (parentNode.type === REGISTRY.route.type && node.type === REGISTRY.condition.type) {if (parentNode.conditionNodes.length === 2) {oldChildNode = parentNode.childNode;parentNode = findParent(parentNode.prevId);} else {for (let i = parentNode.conditionNodes.length - 1; i >= 0; i -= 1) {if (parentNode.conditionNodes[i].nodeId === node.nodeId) {parentNode.conditionNodes.splice(i, 1);isDelCondition = true;break;}}}}if (!isDelCondition) {parentNode.childNode = oldChildNode;if (oldChildNode) {oldChildNode.prevId = parentNode.nodeId;}}this.setState({flow: {...flow}});};
/* 编辑节点 */handleOpenSetting = () => {if (this.isDelClick) {this.isDelClick = false;return;}const { data } = this.state;approverSetting.open({value: data,onOk: (result) => {this.setState({data: result.value});// 不纯洁的代码_.setWith(this.props.config, 'properties', Approver.propertiesEncode(result.value));}});};
修改后代码
/* 类 */class Approver {constructor (prevId) {if (!prevId) throw new Error('Approver node prevId necessary.');this.type = REGISTRY.approver.type;this.name = APPROVAL_TITLE;this.nodeId = uniqueID();this.prevId = prevId;this.properties = {};this.childNode = undefined;this.parentNode = undefined;}addChildNode = node => {const childNode = this.childNode;switch (node.type) {/* 路由节点 */case nodeTypes.ROUTE:this.childNode = node;node.parentNode = this;if (childNode) {this.childNode.childNode = childNode;childNode.parentNode = node;}break;/* 普通节点 */default:this.childNode = node;node.parentNode = this;if (childNode) {this.childNode.childNode = childNode;childNode.parentNode = node;}break;}}delete = () => {if (this.childNode) {this.parentNode.childNode = this.childNode;this.childNode.parentNode = this.parentNode;} else {this.parentNode.childNode = undefined;}}update = properties => {this.properties = properties;}submit() {this.parentNode = null;this.addChildNode = null;this.delete = null;this.update = null;if (this.childNode && this.childNode.submit) this.childNode.submit();}decode = (config) => {/* ... */};encode = (data) => {/* ... */}}export default Approver;
/* 增删节点 */handleNodeDelete = (node) => {const { forceUpdate } = this;node.delete();forceUpdate();};handleNodeAdd = (type, node) => {const { forceUpdate } = this;const newChildNode = new classMap[type](node.nodeId);node.addChildNode(newChildNode);forceUpdate();};
