cpp: Composite Pattern

 

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*****************************************************************//**
 * \file   GoldDir.h
 * \brief  组合模式CompositePattern 亦称: 对象树、Object Tree、Composite c++14
 * 2023年5月28日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef GOLDDIR_H
#define GOLDDIR_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "GoldFile.h"
 
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuCompositePattern
{
 
    /// <summary>
    /// 目录相关类
    /// </summary>
    class GoldDir
    {
    public:
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        GoldDir(string name) :mSname(name) {}
 
    public:
        /// <summary>
        /// 目录中可以增加其他文件
        /// </summary>
        /// <param name="pfile"></param>
        void AddFile(GoldFile* pfile)
        {
            mChildFile.push_back(pfile);
        }
 
        /// <summary>
        /// 目录中可以增加其他目录
        /// </summary>
        /// <param name="pdir"></param>
        void AddDir(GoldDir* pdir)
        {
            mChildDir.push_back(pdir);
        }
 
        /// <summary>
        /// 显示目录名,同时也要负责其下面的文件和目录名的显示工作
        /// </summary>
        /// <param name="lvlstr"></param>
        void ShowName(string lvlstr) //lvlstr:为了显示层次关系的缩进字符串内容
        {
            //(1)输出本目录名
            cout << lvlstr << "+" << mSname << endl;//显示"+"代表是一个目录,其中会包含其他内容
            //(2)输出所包含的文件名
            lvlstr += "    "; //本目录中的文件和目录的显示,要缩进一些来显示
            for (auto iter = mChildFile.begin(); iter != mChildFile.end(); ++iter)
            {
                (*iter)->ShowName(lvlstr); //显示文件名
            }
            //(3)输出所包含的目录名
            for (auto iter = mChildDir.begin(); iter != mChildDir.end(); ++iter)
            {
                (*iter)->ShowName(lvlstr); //显示目录名,这里涉及到了递归调用
            }
        }
 
    private:
        /// <summary>
        /// 目录名
        /// </summary>
        string       mSname;      
        /// <summary>
        /// 目录中包含的文件列表,记得在源文件上面增加代码 #include <list>
        /// </summary>
        list<GoldFile*>  mChildFile;  
        /// <summary>
        /// 目录中包含的子目录列表
        /// </summary>
        list<GoldDir*>   mChildDir;  
 
 
    };
 
}
 
#endif
 
/*****************************************************************//**
 * \file   GoldFile.h
 * \brief  组合模式CompositePattern 亦称: 对象树、Object Tree、Composite c++14
 * 2023年5月28日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef GOLDFILE_H
#define GOLDFILE_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
 
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuCompositePattern
{
    /// <summary>
    /// 文件相关类
    /// </summary>
    class GoldFile
    {
 
    public:
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        GoldFile(string name) :mSname(name) {}
 
        /// <summary>
        /// 显示文件名
        /// </summary>
        /// <param name="lvlstr"></param>
        void ShowName(string lvlstr)
        {
            cout << lvlstr << "-" << mSname << endl; //显示"-"代表是一个文件,属末端节点(不会再有子节点)
        }
    private:
        string mSname;  //文件名
 
 
    };
 
}
 
#endif
 
/*****************************************************************//**
 * \file   PearFile.h
 * \brief  组合模式CompositePattern 亦称: 对象树、Object Tree、Composite c++14
 * 2023年5月28日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef PEARFILE_H
#define PEARFILE_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "PearFileSystem.h"
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuPearCompositePattern
{
 
    /// <summary>
    /// 文件相关类
    /// </summary>
    class PearFile:public PearFileSystem
    {
    public:
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        PearFile(string name) :mSname(name) {}
        /// <summary>
        ///
        /// </summary>
        /// <param name="level"></param>
        void ShowName(int level)
        {
            for (int i = 0; i < level; ++i) { cout << "    "; } //显示若干个空格用与对齐
            cout << "-" << mSname << endl;
        }
        /// <summary>
        ///添加
        /// </summary>
        /// <param name="pfilesys"></param>
        /// <returns></returns>
        virtual int Add(PearFileSystem* pfilesys)
        {
            //文件中其实是不可以增加其他文件或者目录的,所以这里直接返回-1,无奈的是父类中定义了该接口,所以子类中也必须实现该接口
            return -1;
        }
        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="pfilesys"></param>
        /// <returns></returns>
        virtual int Remove(PearFileSystem* pfilesys)
        {
            //文件中不包含其他文件或者目录,所以这里直接返回-1
            return -1;
        }
    private:
        /// <summary>
        /// 文件名
        /// </summary>
        string mSname; 
 
 
    };
}
#endif
 
/*****************************************************************//**
 * \file   PearDir.h
 * \brief  组合模式CompositePattern 亦称: 对象树、Object Tree、Composite c++14
 * 2023年5月28日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef PEARDIR_H
#define PEARDIR_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "PearFileSystem.h"
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuPearCompositePattern
{
 
    /// <summary>
    /// 目录相关类
    /// </summary>
    class PearDir:public PearFileSystem
    {
    public:
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        PearDir(string name) :mSname(name) {}
        /// <summary>
        /// 显示名字
        /// </summary>
        /// <param name="level"></param>
        void ShowName(int level)
        {
            //(1)显示若干个空格用与对齐
            for (int i = 0; i < level; ++i) { cout << "    "; }
            //(2)输出本目录名
            cout << "+" << mSname << endl;
            //(3)显示的层级向下走一级
            level++;
            //(4)输出所包含的子内容(可能是文件,也可能是子目录)  
            //遍历目录中的文件和子目录
            for (auto iter = mChild.begin(); iter != mChild.end(); ++iter)
            {
                (*iter)->ShowName(level);
            }//end for
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="pfilesys"></param>
        /// <returns></returns>
        virtual int Add(PearFileSystem* pfilesys)
        {
            mChild.push_back(pfilesys);
            return 0;
        }
        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="pfilesys"></param>
        /// <returns></returns>
        virtual int Remove(PearFileSystem* pfilesys)
        {
            mChild.remove(pfilesys);
            return 0;
        }
    private:
        /// <summary>
        /// 文件名
        /// </summary>
        string mSname;     
        /// <summary>
        /// 目录中包含的文件或其他目录列表
        /// </summary>
        list<PearFileSystem*> mChild;  //
 
 
    };
 
}
 
#endif
 
/*****************************************************************//**
 * \file   PearFileSystem.h
 * \brief  组合模式CompositePattern 亦称: 对象树、Object Tree、Composite c++14
 * 2023年5月28日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef PEARFILESYSTEM_H
#define PEARFILESYSTEM_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
 
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuPearCompositePattern
{
 
    /// <summary>
    /// 抽象父类FileSystem(抽象接口)
    /// </summary>
    class PearFileSystem
    {
    public:
        /// <summary>
        ///
        /// </summary>
        /// <param name="level"></param>
        virtual void ShowName(int level) = 0; //显示名字,参数level用于表示显示的层级,用于显示对齐
        /// <summary>
        /// 向当前目录中增加文件或子目录
        /// </summary>
        /// <param name="pfilesys"></param>
        /// <returns></returns>
        virtual int Add(PearFileSystem* pfilesys) = 0;
        /// <summary>
        /// 从当前目录中移除文件或子目录
        /// </summary>
        /// <param name="pfilesys"></param>
        /// <returns></returns>
        virtual int Remove(PearFileSystem* pfilesys) = 0;
 
        /// <summary>
        /// 做父类时析构函数应该为虚函数
        /// </summary>
        virtual ~PearFileSystem() {}
 
    };
 
}
 
#endif
 
/*****************************************************************//**
 * \file   JadeFile.h
 * \brief  组合模式CompositePattern 亦称: 对象树、Object Tree、Composite c++14
 * 2023年5月28日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef JADEFILE_H
#define JADEFILE_H 
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "JadeFileSystem.h"
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuJadeCompositePattern
{
 
    /// <summary>
    /// 文件相关类
    /// </summary>
    class JadeFile :public JadeFileSystem
    {
 
    public:
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        JadeFile(string name) :mSname(name) {}
 
        /// <summary>
        /// 显示名字
        /// </summary>
        /// <param name="level"></param>
        void ShowName(int level)
        {
            for (int i = 0; i < level; ++i) { cout << "    "; } //显示若干个空格用与对齐
            cout << "-" << mSname << endl;
        }
 
    public:
        virtual int countNumOfFiles()
        {
            return 1; //文件节点,做数量统计时按1计算
        }
 
    private:
        /// <summary>
        /// 文件名
        /// </summary>
        string mSname;
 
    };
 
}
 
#endif
 
/*****************************************************************//**
 * \file   JadeDir.h
 * \brief  组合模式CompositePattern 亦称: 对象树、Object Tree、Composite c++14
 * 2023年5月28日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef JADEDIR_H
#define JADEDIR_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "JadeFileSystem.h"
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuJadeCompositePattern
{
 
    /// <summary>
    ///
    /// </summary>
    class JadeDir:public JadeFileSystem
    {
        //该类内容基本没变,把Add和Remove成员函数前面的virtual修饰符去掉即可。
    public:
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        JadeDir(string name) :mSname(name) {}
        /// <summary>
        /// 显示名字
        /// </summary>
        /// <param name="level"></param>
        void ShowName(int level)
        {
            //(1)显示若干个空格用与对齐
            for (int i = 0; i < level; ++i) { cout << "    "; }
            //(2)输出本目录名
            cout << "+" << mSname << endl;
            //(3)显示的层级向下走一级
            level++;
            //(4)输出所包含的子内容(可能是文件,也可能是子目录)  
            //遍历目录中的文件和子目录
            for (auto iter = mChild.begin(); iter != mChild.end(); ++iter)
            {
                (*iter)->ShowName(level);
            }//end for
        }
        /*virtual*/
        int Add(JadeFileSystem* pfilesys)
        {
            mChild.push_back(pfilesys);
            return 0;
        }
        /*virtual*/
         
        int Remove(JadeFileSystem* pfilesys)
        {
            mChild.remove(pfilesys);
            return 0;
        }
 
    public:
        virtual JadeDir* ifCompositeObj() { return this; }
 
    public:
        virtual int countNumOfFiles()
        {
            int iNumOfFiles = 0;
            for (auto iter = mChild.begin(); iter != mChild.end(); ++iter)
            {
                iNumOfFiles += (*iter)->countNumOfFiles();//递归调用
            }
            return iNumOfFiles;
        }
 
 
    private:
        /// <summary>
        /// 文件名
        /// </summary>
        string mSname;     
        /// <summary>
        ///
        /// </summary>
        list<JadeFileSystem*> mChild;
 
 
    };
}
#endif
 
/*****************************************************************//**
 * \file   JadeFileSystem.h
 * \brief  组合模式CompositePattern 亦称: 对象树、Object Tree、Composite c++14
 * 2023年5月28日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef JADEFILESYSTEM_H
#define JADEFILESYSTEM_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
 
 
using namespace std;
 
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuJadeCompositePattern
{
    class JadeDir;
    /// <summary>
    ///
    /// </summary>
    class JadeFileSystem
    {
    public:
 
        /// <summary>
        /// 显示名字,参数level用于表示显示的层级,用于显示对齐       
        /// </summary>
        /// <param name="level"></param>
        virtual void ShowName(int level) = 0;
        /// <summary>
        /// 做父类时析构函数应该为虚函数
        /// </summary>
        virtual ~JadeFileSystem() {}
 
    public:
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        virtual JadeDir* ifCompositeObj() { return nullptr; }
 
    public:
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        virtual int countNumOfFiles() = 0;  //统计目录下包含的文件个数
 
 
    };
}
#endif

  

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*****************************************************************//**
 * \file   GeovinDu.h
 * \brief  组合模式CompositePattern 亦称: 对象树、Object Tree、Composite c++14
 * 2023年5月28日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef GEOVINDU_H
#define GEOVINDU_H
 
 
 
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "GoldFile.h"
#include "GoldDir.h"
#include "PearFileSystem.h"
#include "PearDir.h"
#include "PearFile.h"
 
#include "JadeDir.h"
#include "JadeFile.h"
#include "JadeFileSystem.h"
 
 
using namespace std;
using namespace DuPearCompositePattern;
using namespace DuJadeCompositePattern;
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuCompositePattern
{
 
    /// <summary>
    ///
    /// </summary>
    class GeovinDu
    {
 
    private:
 
    public:
 
        /// <summary>
        ///示例
        /// </summary>
        void displaySimple();
        /// <summary>
        ///
        /// </summary>
        void displayPearSimple();
 
        /// <summary>
        ///
        /// </summary>
        void displayJadeSimple();
    };
}
#endif
 
/*****************************************************************//**
 * \file   GeovinDu.cpp
 * \brief  组合模式CompositePattern 亦称: 对象树、Object Tree、Composite c++14
 * 2023年5月28日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#include "GeovinDu.h"
 
using namespace std;
using namespace DuPearCompositePattern;
using namespace DuJadeCompositePattern;
 
 
/**
* @brief 类库空间名
* geovindu edit
*
*/
namespace DuCompositePattern
{
 
    /// <summary>
    /// 示例
    /// </summary>
    void GeovinDu::displaySimple()
    {
 
 
        //(1)创建各种目录,文件对象
        DuCompositePattern::GoldDir* pdir1 = new DuCompositePattern::GoldDir("root");
        //-------
        DuCompositePattern::GoldFile* pfile1 = new DuCompositePattern::GoldFile("common.mk");
        DuCompositePattern::GoldFile* pfile2 = new DuCompositePattern::GoldFile("config.mk");
        DuCompositePattern::GoldFile* pfile3 = new DuCompositePattern::GoldFile("makefile");
        //-------
        DuCompositePattern::GoldDir* pdir2 = new DuCompositePattern::GoldDir("app");
        DuCompositePattern::GoldFile* pfile4 = new DuCompositePattern::GoldFile("nginx.c");
        DuCompositePattern::GoldFile* pfile5 = new DuCompositePattern::GoldFile("ngx_conf.c");
        //-------
        DuCompositePattern::GoldDir* pdir3 = new DuCompositePattern::GoldDir("signal");
        DuCompositePattern::GoldFile* pfile6 = new DuCompositePattern::GoldFile("ngx_signal.c");
        //-------
        DuCompositePattern::GoldDir* pdir4 = new DuCompositePattern::GoldDir("_include");
        DuCompositePattern::GoldFile* pfile7 = new DuCompositePattern::GoldFile("ngx_func.h");
        DuCompositePattern::GoldFile* pfile8 = new DuCompositePattern::GoldFile("ngx_signal.h");
 
        //(2)构造树形目录结构
        pdir1->AddFile(pfile1);
        pdir1->AddFile(pfile2);
        pdir1->AddFile(pfile3);
        //-------
        pdir1->AddDir(pdir2);
        pdir2->AddFile(pfile4);
        pdir2->AddFile(pfile5);
        //-------
        pdir1->AddDir(pdir3);
        pdir3->AddFile(pfile6);
        //-------
        pdir1->AddDir(pdir4);
        pdir4->AddFile(pfile7);
        pdir4->AddFile(pfile8);
 
        //(3)输出整个目录结构,只要调用根目录的ShowName方法即可,每个子目录都有自己的ShowName方法负责自己旗下的文件和目录的显示
        pdir1->ShowName("");//缩进字符刚开始可以为空
 
        //(4)释放资源
        delete pfile8;
        delete pfile7;
        delete pdir4;
        //-------
        delete pfile6;
        delete pdir3;
        //-------
        delete pfile5;
        delete pfile4;
        delete pdir2;
        //-------
        delete pfile3;
        delete pfile2;
        delete pfile1;
        delete pdir1;
         
 
 
    }
 
    /// <summary>
    ///
    /// </summary>
    void GeovinDu::displayPearSimple()
    {
 
        //(1)创建各种目录,文件对象
        DuPearCompositePattern::PearFileSystem* pdir1 = new DuPearCompositePattern::PearDir("root");
        //-------
        DuPearCompositePattern::PearFileSystem* pfile1 = new DuPearCompositePattern::PearFile("common.mk");
        DuPearCompositePattern::PearFileSystem* pfile2 = new DuPearCompositePattern::PearFile("config.mk");
        DuPearCompositePattern::PearFileSystem* pfile3 = new DuPearCompositePattern::PearFile("makefile");
        //-------
        DuPearCompositePattern::PearFileSystem* pdir2 = new DuPearCompositePattern::PearDir("app");
        DuPearCompositePattern::PearFileSystem* pfile4 = new DuPearCompositePattern::PearFile("nginx.c");
        DuPearCompositePattern::PearFileSystem* pfile5 = new DuPearCompositePattern::PearFile("ngx_conf.c");
        //-------
        DuPearCompositePattern::PearFileSystem* pdir3 = new DuPearCompositePattern::PearDir("signal");
        DuPearCompositePattern::PearFileSystem* pfile6 = new DuPearCompositePattern::PearFile("ngx_signal.c");
        //-------
        DuPearCompositePattern::PearFileSystem* pdir4 = new DuPearCompositePattern::PearDir("_include");
        DuPearCompositePattern::PearFileSystem* pfile7 = new DuPearCompositePattern::PearFile("ngx_func.h");
        DuPearCompositePattern::PearFileSystem* pfile8 = new DuPearCompositePattern::PearFile("ngx_signal.h");
 
        //(2)构造树形目录结构
        pdir1->Add(pfile1);
        pdir1->Add(pfile2);
        pdir1->Add(pfile3);
        //-------
        pdir1->Add(pdir2);
        pdir2->Add(pfile4);
        pdir2->Add(pfile5);
        //-------
        pdir1->Add(pdir3);
        pdir3->Add(pfile6);
        //-------
        pdir1->Add(pdir4);
        pdir4->Add(pfile7);
        pdir4->Add(pfile8);
 
        //(3)输出整个目录结构,只要调用根目录的ShowName方法即可,每个子目录都有自己的ShowName方法负责自己旗下的文件和目录的显示 
        pdir1->ShowName(0);
 
        //(4)释放资源
        delete pfile8;
        delete pfile7;
        delete pdir4;
        //-------
        delete pfile6;
        delete pdir3;
        //-------
        delete pfile5;
        delete pfile4;
        delete pdir2;
        //-------
        delete pfile3;
        delete pfile2;
        delete pfile1;
        delete pdir1;
 
    }
    /// <summary>
    ///
    /// </summary>
    void GeovinDu::displayJadeSimple()
    {
        //(1)创建各种目录,文件对象
        DuJadeCompositePattern::JadeDir* pdir1 = new DuJadeCompositePattern::JadeDir("root");
        //-------
        DuJadeCompositePattern::JadeFileSystem* pfile1 = new DuJadeCompositePattern::JadeFile("common.mk");
        DuJadeCompositePattern::JadeFileSystem* pfile2 = new DuJadeCompositePattern::JadeFile("config.mk");
        DuJadeCompositePattern::JadeFileSystem* pfile3 = new DuJadeCompositePattern::JadeFile("makefile");
        //-------
        DuJadeCompositePattern::JadeDir* pdir2 = new DuJadeCompositePattern::JadeDir("app");
        if (pdir2->ifCompositeObj() != nullptr)
        {
            //是个组合对象,可以向其中增加单个对象和其它组合对象
        }
        if (dynamic_cast<DuJadeCompositePattern::JadeDir*>(pdir2) != nullptr)
        {
            //是个组合对象,可以向其中增加单个对象和其它组合对象
        }
 
        DuJadeCompositePattern::JadeFileSystem* pfile4 = new DuJadeCompositePattern::JadeFile("nginx.c");
        DuJadeCompositePattern::JadeFileSystem* pfile5 = new DuJadeCompositePattern::JadeFile("ngx_conf.c");
        //-------
        DuJadeCompositePattern::JadeDir* pdir3 = new DuJadeCompositePattern::JadeDir("signal");
        DuJadeCompositePattern::JadeFileSystem* pfile6 = new DuJadeCompositePattern::JadeFile("ngx_signal.c");
        //-------
        DuJadeCompositePattern::JadeDir* pdir4 = new DuJadeCompositePattern::JadeDir("_include");
        DuJadeCompositePattern::JadeFileSystem* pfile7 = new DuJadeCompositePattern::JadeFile("ngx_func.h");
        DuJadeCompositePattern::JadeFileSystem* pfile8 = new DuJadeCompositePattern::JadeFile("ngx_signal.h");
 
        //(2)构造树形目录结构
        //这部分内容基本没变,
        pdir1->Add(pfile1);
        pdir1->Add(pfile2);
        pdir1->Add(pfile3);
        //-------
        pdir1->Add(pdir2);
        pdir2->Add(pfile4);
        pdir2->Add(pfile5);
        //-------
        pdir1->Add(pdir3);
        pdir3->Add(pfile6);
        //-------
        pdir1->Add(pdir4);
        pdir4->Add(pfile7);
        pdir4->Add(pfile8);
 
        //(3)输出整个目录结构,只要调用根目录的ShowName方法即可,每个子目录都有自己的ShowName方法负责自己旗下的文件和目录的显示
        pdir1->ShowName(0);
 
        cout << "文件的数量为:" << pdir1->countNumOfFiles() << endl;
 
        //(4)释放资源
        //这部分内容基本没变,
        delete pfile8;
        delete pfile7;
        delete pdir4;
        //-------
        delete pfile6;
        delete pdir3;
        //-------
        delete pfile5;
        delete pfile4;
        delete pdir2;
        //-------
        delete pfile3;
        delete pfile2;
        delete pfile1;
        delete pdir1;
 
 
    }
 
}

  

调用:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*****************************************************************//**
 * \file   ConsoleDuCompositePattern.cpp
 * \brief  组合模式CompositePattern 亦称: 对象树、Object Tree、Composite c++14
 * 2023年5月28日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
 
 
// ConsoleDuCompositePattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//2023年5月22日 涂聚文 Geovin Du Visual Studio 2022 edit.
#define _UNICODE
 
#include <iostream>
#include <vector>
#include <list>
#include "GeovinDu.h"
 
#ifdef _DEBUG   //只在Debug(调试)模式下
#ifndef DEBUG_NEW
#define DEBUG_NEW new(_NORMAL_BLOCK,__FILE__,__LINE__) //重新定义new运算符
#define new DEBUG_NEW
#endif
#endif
 
using namespace std;
using namespace DuCompositePattern;
 
int main()
{
    
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);//程序退出时检测内存泄漏并显示到“输出”窗口
    std::cout << "Hello World!涂聚文 Geovin Du\n";
 
    GeovinDu geovin;
    geovin.displaySimple();
    cout << "*********" << endl;
    geovin.displayPearSimple();
    cout << "*********" << endl;
    geovin.displayJadeSimple();
 
    system("pause");
    return 0;
 
}
 
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
 
// 入门使用技巧:
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
#define UNICODE

  

输出:

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
40
41
Hello World!涂聚文 Geovin Du
+root
    -common.mk
    -config.mk
    -makefile
    +app
        -nginx.c
        -ngx_conf.c
    +signal
        -ngx_signal.c
    +_include
        -ngx_func.h
        -ngx_signal.h
*********
+root
    -common.mk
    -config.mk
    -makefile
    +app
        -nginx.c
        -ngx_conf.c
    +signal
        -ngx_signal.c
    +_include
        -ngx_func.h
        -ngx_signal.h
*********
+root
    -common.mk
    -config.mk
    -makefile
    +app
        -nginx.c
        -ngx_conf.c
    +signal
        -ngx_signal.c
    +_include
        -ngx_func.h
        -ngx_signal.h
文件的数量为:8
请按任意键继续. . .

  

 

posted @   ®Geovin Du Dream Park™  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2019-05-28 Python:GUI
2015-05-28 sql: Oracle simple example table
2014-05-28 csharp: Request.Form,Request.QueryString,Request.Params,Request.Cookies
2011-05-28 Csharp 两个DataTable或DataView互换,可以用来加密解密
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示