组合模式(Composite Pattern)
1 2 3 |
定义:又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。 目的:将对象组合成树形结构以表示"部分-整体"的层次结构。 场景:您想表示对象的部分-整体层次结构(树形结构),如:文件系统。 |
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 |
// 指令 let directive = { eat: { excute: () => { console.log('eat') } }, sleep: { excute: () => { console.log('sleep') } }, code: { excute: () => { console.log('Get out there and write code!') } } } const callDirective = () => { let result = { directiveList: [], add: (directive) => { result.directiveList.push(directive) }, excute: () => { for (let ele of result.directiveList.values()) { ele.excute() } } } return result } let command = callDirective() command.add(directive.eat) command.add(directive.sleep) command.add(directive.code) command.excute() |
Github地址:https://github.com/skillnull/Design-Mode-Example[……]