File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change 1- // 组合模式(Composite Pattern)
1+ // 组合模式(Composite Pattern)
2+
3+ // 指令
4+ let directive = {
5+ eat : {
6+ excute : ( ) => {
7+ console . log ( 'eat' )
8+ }
9+ } ,
10+ sleep : {
11+ excute : ( ) => {
12+ console . log ( 'sleep' )
13+ }
14+ } ,
15+ code : {
16+ excute : ( ) => {
17+ console . log ( 'Get out there and write code!' )
18+ }
19+ }
20+ }
21+
22+ const callDirective = ( ) => {
23+ let result = {
24+ directiveList : [ ] ,
25+ add : ( directive ) => {
26+ result . directiveList . push ( directive )
27+ } ,
28+ excute : ( ) => {
29+ for ( let ele of result . directiveList . values ( ) ) {
30+ ele . excute ( )
31+ }
32+ }
33+ }
34+ return result
35+ }
36+
37+ let command = callDirective ( )
38+ command . add ( directive . eat )
39+ command . add ( directive . sleep )
40+ command . add ( directive . code )
41+ command . excute ( )
Original file line number Diff line number Diff line change 5656
5757 定义:又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。
5858 目的:将对象组合成树形结构以表示"部分-整体"的层次结构。
59- 场景:您想表示对象的部分-整体层次结构( 树形结构) 。
59+ 场景:您想表示对象的部分-整体层次结构( 树形结构),如:文件系统 。
6060
6161> [ 装饰模式(Decorator Pattern)] ( /Example/Decorator-Pattern.js )
6262
You can’t perform that action at this time.
0 commit comments