组合模式(Composite Pattern)
定义:又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。目的:将对象组合成树形结构以表示"部分-整体"的层次结构。场景:您想表示对象的部分-整体层次结构(树形结构),如:文件系统。
// 指令 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()
Git地址:https://github.com/skillnull/Design-Mode-Example
原文地址:https://www.cnblogs.com/Man-Dream-Necessary/p/12419391.html
时间: 2024-09-30 05:19:35