cpp: Visitor 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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
/*****************************************************************//**
 * \file   Gold.h
 * \brief 访问者模式  Visitor Pattern  C++ 14 行为模式
 *  2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLD_H
#define GOLD_H
 
#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>
#include <map>
 
 
 
using namespace std;
 
 
 
namespace DuJewelryVisitorPattern
{
 
    class GoldVisitor;
    /// <summary>
    /// 黄金
    /// </summary>
    class Gold
    {
 
    public:
 
        /// <summary>
        /// 这里的形参是访问者父类指针
        /// </summary>
        /// <param name="pvisitor"></param>
        virtual void Accept(GoldVisitor* pvisitor) = 0;
 
    public:
        /// <summary>
        /// 名称
        /// </summary>
        /// <returns></returns>
        virtual string getName() = 0; 
 
        /// <summary>
        /// 总价格,单位:元
        /// </summary>
        /// <returns></returns>
        virtual float getPrice() = 0;   
 
    };
 
}
 
#endif
 
 
/*****************************************************************//**
 * \file   GoldGenius.h
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLDGENIUS_H
#define GOLDGENIUS_H
 
#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>
 
#include "Gold.h"
#include "GoldVisitor.h"
 
 
using namespace std;
 
 
 
 
namespace DuJewelryVisitorPattern
{
 
     
 
    /// <summary>
    /// 天赋
    /// </summary>
    class GoldGenius:public Gold
    {
    public:
 
        /// <summary>
        /// 名称
        /// </summary>
        /// <returns></returns>
        virtual string getName()
        {
            return "天赋";
        }
 
        /// <summary>
        /// 价格
        /// </summary>
        /// <returns></returns>
        virtual float getPrice()
        {
            return 46.8f;  
        }
 
    public:
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="pvisitor"></param>
        void Accept(GoldVisitor* pvisitor);
     
 
 
    };
}
#endif
 
 
/*****************************************************************//**
 * \file   GoldGentle.h
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLDGENGLE_H
#define GOLDGENGLE_H
 
#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>
 
#include "Gold.h"
#include "GoldVisitor.h"
 
using namespace std;
 
 
 
 
namespace DuJewelryVisitorPattern
{
 
    /// <summary>
    /// 高尚
    /// </summary>
    class GoldGentle :public Gold
    {
 
    public:
 
        /// <summary>
        /// 名称
        /// </summary>
        /// <returns></returns>
        virtual string getName()
        {
            return "高尚";
        }
 
        /// <summary>
        /// 价格
        /// </summary>
        /// <returns></returns>
        virtual float getPrice()
        {
            return 111.3f;   
        }
 
    public:
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="pvisitor"></param>
        void Accept(GoldVisitor* pvisitor);
 
 
    };
 
}
 
#endif
 
/*****************************************************************//**
 * \file   GoldGlorious.h
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLDGLORIOUS_H
#define GOLDGLORIOUS_H
 
#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>
 
#include "Gold.h"
#include "GoldVisitor.h"
 
 
using namespace std;
 
 
 
 
namespace DuJewelryVisitorPattern
{
 
    /// <summary>
    /// 璀璨
    /// </summary>
    class GoldGlorious :public Gold
    {
    public:
 
        /// <summary>
        /// 名称
        /// </summary>
        /// <returns></returns>
        virtual string getName()
        {
            return "璀璨";
        }
 
        /// <summary>
        /// 价格
        /// </summary>
        /// <returns></returns>
        virtual float getPrice()
        {
            return 122.0f;    //
        }
 
    public:
 
        /// <summary>
        /// virtual
        /// </summary>
        /// <param name="pvisitor"></param>
         void Accept(GoldVisitor* pvisitor) ; //
 
 
    };
}
#endif
 
/*****************************************************************//**
 * \file   GoldGenius.cpp
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#include "GoldGenius.h"
 
using namespace std;
 
 
 
 
namespace DuJewelryVisitorPattern
{
    /// <summary>
    ///
    /// </summary>
    /// <param name="pvisitor"></param>
    void GoldGenius::Accept(GoldVisitor* pvisitor)
    {
        pvisitor->VisitElmGenius(this);
    }
}
 
/*****************************************************************//**
 * \file   GoldGentle.cpp
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#include "GoldGentle.h"
using namespace std;
 
 
 
 
namespace DuJewelryVisitorPattern
{
 
 
    /// <summary>
    ///
    /// </summary>
    /// <param name="pvisitor"></param>
    void GoldGentle::Accept(GoldVisitor* pvisitor)
    {
        pvisitor->VisitElmGentle(this);
         
    }
}
 
/*****************************************************************//**
 * \file   GoldGlorious.cpp
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#include "GoldGlorious.h"
using namespace std;
 
 
 
 
namespace DuJewelryVisitorPattern
{
 
    /// <summary>
    ///
    /// </summary>
    /// <param name="pvisitor"></param>
    void GoldGlorious::Accept(GoldVisitor* pvisitor)
    {
        pvisitor->VisitElmGlorious(this);
    }
 
}
 
/*****************************************************************//**
 * \file   GoldVisitor.h
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLDVISITOR_H
#define GOLDVISITOR_H
 
#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>
#include <map>
 
#include "GoldGenius.h"
#include "GoldGentle.h"
#include "GoldGlorious.h"
 
using namespace std;
 
 
 
namespace DuJewelryVisitorPattern
{
 
    class GoldGenius;
    class GoldGentle;
    class GoldGlorious;
 
 
    /// <summary>
    ///
    /// </summary>
    class GoldVisitor
    {
 
    public:
        /// <summary>
        /// 做父类时析构函数应该为虚函数
        /// </summary>
        virtual ~GoldVisitor() {}
        /// <summary>
        /// 访问元素: virtual  = 0
        /// </summary>
        /// <param name="pelem"></param>
        virtual void VisitElmGenius(GoldGenius* pelem)=0 ;
        /// <summary>
        /// 访问元素:
        /// </summary>
        /// <param name="pelem"></param>
        virtual void VisitElmGentle(GoldGentle* pelem)=0;
        /// <summary>
        /// 访问元素:
        /// </summary>
        /// <param name="pelem"></param>
        virtual void VisitElmGlorious(GoldGlorious* pelem)=0;
 
        ////以下几个接口的名字都叫Visit(成员函数重载)
        //virtual void Visit(GoldGenius* pelem) = 0;   //访问元素:
        //virtual void Visit(GoldGentle* pelem) = 0;  //访问元素:
        //virtual void Visit(GoldGlorious* pelem) = 0;       //访问元素:
    };
 
}
 
#endif
 
/*****************************************************************//**
 * \file   ObjectStructure.h
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef OBJECTSTRUCTURE_H
#define OBJECTSTRUCTURE_H
 
#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>
 
#include "Gold.h"
#include "GoldVisitor.h"
#include "GoldGenius.h"
#include "GoldGentle.h"
#include "GoldGlorious.h"
 
 
 
using namespace std;
 
 
 
 
namespace DuJewelryVisitorPattern
{
 
     
    /// <summary>
    /// 对象结构
    /// </summary>
    class ObjectStructure
    {
 
    public:
        /// <summary>
        /// 增加黄金到黄金列表中
        /// </summary>
        /// <param name="p_mdc"></param>
        void addGold(Gold* p_mdc)
        {
            goldlist.push_back(p_mdc);
        }
        /// <summary>
        /// /
        /// </summary>
        /// <param name="pvisitor"></param>
        void procAction(GoldVisitor* pvisitor)
        {
            for (auto iter = goldlist.begin(); iter != goldlist.end(); ++iter)
            {
                (*iter)->Accept(pvisitor);
 
            }
        }
 
 
 
 
    private:
 
        /// <summary>
        /// 黄金列表
        /// </summary>
        list <Gold*> goldlist;
 
    };
}
#endif
 
 
/*****************************************************************//**
 * \file   VisitorConsignee.h
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef VISITORCONSIGNEE_H
#define VISITORCONSIGNEE_H
 
#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>
 
#include "Gold.h"
#include "GoldVisitor.h"
#include "GoldGenius.h"
#include "GoldGentle.h"
#include "GoldGlorious.h"
 
 
 
using namespace std;
 
 
 
 
namespace DuJewelryVisitorPattern
{
 
    /// <summary>
    /// 取黄金人员
    /// </summary>
    class VisitorConsignee : public GoldVisitor
    {
    public:
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="pelem"></param>
        virtual void VisitElmGenius(GoldGenius* pelem)
        {
            cout << "取黄金人员将黄金“" << pelem->getName() << "”拿给了我!" << endl;
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="pelem"></param>
        virtual void VisitElmGentle(GoldGentle* pelem)
        {
            cout << "取黄金人员将黄金“" << pelem->getName() << "”拿给了我!" << endl;
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="pelem"></param>
        virtual void VisitElmGlorious(GoldGlorious* pelem)
        {
            cout << "取黄金人员将黄金“" << pelem->getName() << "”拿给了我!" << endl;
        }
 
    };
 
}
 
#endif
 
/*****************************************************************//**
 * \file   VisitorCounselor.h
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef VISITORCOUNSELOR_H
#define VISITORCOUNSELOR_H
 
#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>
 
#include "Gold.h"
#include "GoldVisitor.h"
#include "GoldGenius.h"
#include "GoldGentle.h"
#include "GoldGlorious.h"
 
 
 
using namespace std;
 
 
 
 
namespace DuJewelryVisitorPattern
{
 
    /// <summary>
    /// 珠宝顾问
    /// </summary>
    class VisitorCounselor : public GoldVisitor
    {
 
    public:
 
        /// <summary>
        /// 天赋
        /// </summary>
        /// <param name="pelem"></param>
        virtual void VisitElmGenius(GoldGenius* pelem)
        {
            cout << "珠宝顾问建议:“天赋”!" << endl;
        }
 
        /// <summary>
        /// 高尚
        /// </summary>
        /// <param name="pelem"></param>
        virtual void VisitElmGentle(GoldGentle* pelem)
        {
            cout << "珠宝顾问建议:“高尚”!" << endl;
        }
 
        /// <summary>
        /// 璀璨
        /// </summary>
        /// <param name="pelem"></param>
        virtual void VisitElmGlorious(GoldGlorious* pelem)
        {
            cout << "珠宝顾问建议:“璀璨”!" << endl;
        }
 
    };
}
#endif
 
/*****************************************************************//**
 * \file   VisitorTollCollector.h
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef VISITORTOLLCOLLECTOR_H
#define VISITORTOLLCOLLECTOR_H
 
#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>
 
#include "Gold.h"
#include "GoldVisitor.h"
#include "GoldGenius.h"
#include "GoldGentle.h"
#include "GoldGlorious.h"
 
 
 
using namespace std;
 
 
 
 
namespace DuJewelryVisitorPattern
{
 
    /// <summary>
    /// 收费人员访问者子类
    /// </summary>
    class VisitorTollCollector : public GoldVisitor
    {
    public:
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="pelem"></param>
        virtual void VisitElmGenius(GoldGenius* pelem)
        {
            float tmpprice = pelem->getPrice();
            cout << "收费人员累计黄金“" << pelem->getName() << "”的价格:" << tmpprice << endl;
            totalCost += tmpprice;
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="pelem"></param>
        virtual void VisitElmGentle(GoldGentle* pelem)
        {
            float tmpprice = pelem->getPrice();
            cout << "收费人员累计黄金“" << pelem->getName() << "”的价格:" << tmpprice << endl;
            totalCost += tmpprice;
        }
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="pelem"></param>
        virtual void VisitElmGlorious(GoldGlorious* pelem)
        {
            float tmpprice = pelem->getPrice();
            cout << "收费人员累计黄金“" << pelem->getName() << "”的价格:" << tmpprice << endl;
            totalCost += tmpprice;
        }
 
        /// <summary>
        /// 返回总费用
        /// </summary>
        /// <returns></returns>
        float getTotalCost()
        {
            return totalCost;
        }
    private:
 
        /// <summary>
        /// 总费用
        /// </summary>
        float totalCost = 0.0f; 
 
    };
}
 
 
#endif
 
 
/*****************************************************************//**
 * \file   GeovinDu.h
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GEOVINDU_H
#define GEOVINDU_H
 
#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>
 
#include "Gold.h"
#include "GoldVisitor.h"
#include "GoldGenius.h"
#include "GoldGentle.h"
#include "GoldGlorious.h"
 
#include "GoldProcess.h"
 
#include "VisitorConsignee.h"
#include "VisitorCounselor.h"
#include "VisitorTollCollector.h"
#include "ObjectStructure.h"
 
 
using namespace std;
 
 
 
 
namespace DuJewelryVisitorPattern
{
 
    /// <summary>
    ///
    /// </summary>
    class GeovinDu
    {
    private:
 
 
    public:
 
        /// <summary>
        ///
        /// </summary>
        void displaySimple();
 
        /// <summary>
        ///
        /// </summary>
        void displayDuSimple();
 
 
 
    };
 
}
 
#endif
 
/*****************************************************************//**
 * \file   GeovinDu.cpp
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#include "GeovinDu.h"
 
 
using namespace std;
 
 
 
 
namespace DuJewelryVisitorPattern
{
 
    /// <summary>
    ///
    /// </summary>
    void GeovinDu::displaySimple()
    {
        DuJewelryVisitorPattern::Gold* pm1 = new DuJewelryVisitorPattern::GoldGenius();
        DuJewelryVisitorPattern::Gold* pm2 = new DuJewelryVisitorPattern::GoldGentle();
        DuJewelryVisitorPattern::Gold* pm3 = new DuJewelryVisitorPattern::GoldGlorious();    
 
        DuJewelryVisitorPattern::GoldProcess mdcprocobj;
        mdcprocobj.addGold(pm1);
        mdcprocobj.addGold(pm2);
        mdcprocobj.addGold(pm3);
        mdcprocobj.procAction("收费人员");
        mdcprocobj.procAction("取黄金人员");
 
        //释放资源
        delete pm1;
        delete pm2;
        delete pm3;
 
    }
    /// <summary>
    ///
    /// </summary>
    void GeovinDu::displayDuSimple()
    {
        DuJewelryVisitorPattern::VisitorTollCollector visitorgeovindu; //收费人员访问者子类,里面承载着向我收费的算法
        DuJewelryVisitorPattern::GoldGenius goldasplcrp;
        DuJewelryVisitorPattern::GoldGentle  goldfftdnhsp;
        DuJewelryVisitorPattern::GoldGlorious golddlx;
 
        //各个元素子类调用Accept接受访问者的访问,就可以实现访问者要实现的功能
        goldasplcrp.Accept(&visitorgeovindu); //累加价格
        goldfftdnhsp.Accept(&visitorgeovindu);//累加价格
        golddlx.Accept(&visitorgeovindu);     //累加价格
 
        cout << "所有黄金的总价为:" << visitor_geovindu.getTotalCost() << ",收费人员收取了我的费用!" << endl;
 
        //----
        DuJewelryVisitorPattern::VisitorConsignee visitordu; //取黄金员访问者子类,里面承载着向我发黄金的算法
        goldasplcrp.Accept(&visitordu);
        goldfftdnhsp.Accept(&visitordu);
        golddlx.Accept(&visitordu);    
 
        //-----
        DuJewelryVisitorPattern::VisitorCounselor visitorgeovin;  //珠宝顾问访问者子类,里面承载着为我配置算法
        goldasplcrp.Accept(&visitorgeovin);
        goldfftdnhsp.Accept(&visitorgeovin);
        golddlx.Accept(&visitorgeovin);    
 
 
        //---------
        DuJewelryVisitorPattern::ObjectStructure objstruc;
        objstruc.addGold(&goldasplcrp);
        objstruc.addGold(&goldfftdnhsp);
        objstruc.addGold(&golddlx);
        objstruc.procAction(&visitorgeovin);
 
    }
 
}

  

调用:

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
/*****************************************************************//**
 * \file   ConsoleDuVisitorPattern.cpp
 * \brief  访问者模式  Visitor Pattern  C++ 14 行为模式
 * 2023年6月8日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社
 *
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
// ConsoleDuVisitorPattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _UNICODE
 
 
#include <iostream>
#include "GeovinDu.h"
 
using namespace std;
using namespace DuJewelryVisitorPattern;
 
 
int main()
{
    std::cout << "Hello World!!涂聚文 Geovin Du\n";
    GeovinDu geovin;
    geovin.displaySimple();
 
    cout << "************" << endl;
    geovin.displayDuSimple();
 
 
 
 
    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
Hello World!!涂聚文 Geovin Du
收费人员累计黄金“天赋”的价格:46.8
收费人员累计黄金“高尚”的价格:111.3
收费人员累计黄金“璀璨”的价格:122
所有黄金的总为:280.1,收费人员收取了我的费用!
取黄金人员将黄金“天赋”拿给了我!
取黄金人员将黄金“高尚”拿给了我!
取黄金人员将黄金“璀璨”拿给了我!
************
收费人员累计黄金“天赋”的价格:46.8
收费人员累计黄金“高尚”的价格:111.3
收费人员累计黄金“璀璨”的价格:122
所有黄金的总价为:280.1,收费人员收取了我的费用!
取黄金人员将黄金“天赋”拿给了我!
取黄金人员将黄金“高尚”拿给了我!
取黄金人员将黄金“璀璨”拿给了我!
珠宝顾问建议:“天赋”!
珠宝顾问建议:“高尚”!
珠宝顾问建议:“璀璨”!
珠宝顾问建议:“天赋”!
珠宝顾问建议:“高尚”!
珠宝顾问建议:“璀璨”!
请按任意键继续. . .

  

 

 

posted @   ®Geovin Du Dream Park™  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2011-06-09 CSS Vertical Text(三种方式)
< 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
点击右上角即可分享
微信分享提示