cpp: Chain Of Responsibility 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 | /*****************************************************************/ /** * \file PayrollProcessing.h * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月6日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社 * \author geovindu * \date June 2023 *********************************************************************/ #pragma once #ifndef PAYROLLPROCESSING_H #define PAYROLLPROCESSING_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 DuJewelryChainOfResponsibilityPattern { /// <summary> /// 薪水处理类 /// </summary> class PayrollProcessing { public : /// <summary> /// 处理加薪请求 /// </summary> /// <param name="sname">员工姓名</param> /// <param name="salfigure">加薪多少</param> void raiseSalaryRequest( const string& sname, int salfigure) //参数1代表要加薪的员工名字,参数2代表求要加薪多少 { if (salfigure <= 1000) { //加薪要求不超过1000,部门经理可以批准 depmentManagerApprove(sname, salfigure); } else if (salfigure <= 5000) { //加薪要求在1000元之上但不超过5000,技术总监才能批准 CTOApprove(sname, salfigure); } else { //加薪要求超过5000元,总经理才能批准 generalManagerApprove(sname, salfigure); } } private : /// <summary> /// 部门经理审批加薪请求 /// </summary> /// <param name="sname">员工姓名</param> /// <param name="salfigure">加薪多少</param> void depmentManagerApprove( const string& sname, int salfigure) { cout << sname << "的加薪要求为:" << salfigure << "元,部门经理审批通过!" << endl; } /// <summary> /// 技术总监审批加薪请求 /// </summary> /// <param name="sname">员工姓名</param> /// <param name="salfigure">加薪多少</param> void CTOApprove( const string& sname, int salfigure) { cout << sname << "的加薪要求为:" << salfigure << "元,技术总监审批通过!" << endl; } /// <summary> /// 总经理审批加薪请求 /// </summary> /// <param name="sname">员工姓名</param> /// <param name="salfigure">加薪多少</param> void generalManagerApprove( const string& sname, int salfigure) { cout << sname << "的加薪要求为:" << salfigure << "元,总经理审批通过!" << endl; } }; } #endif /*****************************************************************/ /** * \file ParentSalaryApprover.h * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月7日 涂聚文 Geovin Du Visual Studio 2022 edit. * \author geovindu * \date June 2023 *********************************************************************/ #pragma once #ifndef PARENTSALARYAPPROVER_H #define PARENTSALARYAPPROVER_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 "RaiseSalaryRequest.h" using namespace std; namespace DuChainOfResponsibilityPattern { /// <summary> /// 薪水审批者父类 /// </summary> class ParentSalaryApprover { public : /// <summary> /// /// </summary> ParentSalaryApprover() :nextChain( nullptr ) {} /// <summary> /// 做父类时析构函数应该为虚函数 /// </summary> virtual ~ParentSalaryApprover() {} /// <summary> /// 设置指向的职责链中的下个审批者 /// </summary> /// <param name="next"></param> void setNextChain(ParentSalaryApprover* next) { nextChain = next; } /// <summary> /// 处理加薪请求 /// </summary> /// <param name="req"></param> virtual void processRequest( const RaiseSalaryRequest& req) = 0; protected : /// <summary> /// 找链中的下个对象并把请求投递给下个链中的对象 /// </summary> /// <param name="req"></param> void sendRequestToNextHandler( const RaiseSalaryRequest& req) { //找链中的下个对象 if (nextChain != nullptr ) { //把请求投递给链中的下个对象 nextChain->processRequest(req); } else { //没找到链中的下个对象,程序流程执行这里似乎不应该 cout << req.getName() << "的加薪要求为:" << req.getSalFigure() << "元,但无人能够审批!" << endl; } } private : /// <summary> /// /// </summary> ParentSalaryApprover* nextChain; //指向下一个审批者(对象)的多态指针(指向自身类型),每个都指向下一个,就会构成一个职责链(链表) }; } #endif /*****************************************************************/ /** * \file GeneralManagerApprove.h * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月7日 涂聚文 Geovin Du Visual Studio 2022 edit. * \author geovindu * \date June 2023 *********************************************************************/ #pragma once #ifndef GENERALMANAGERAPPROVER_H #define GENERALMANAGERAPPROVER_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 "RaiseSalaryRequest.h" #include "ParentSalaryApprover.h" using namespace std; namespace DuChainOfResponsibilityPattern { /// <summary> /// 总经理子类 /// </summary> class GeneralManagerApprove : public ParentSalaryApprover { public : /// <summary> /// 处理加薪请求 /// </summary> /// <param name="req"></param> virtual void processRequest( const RaiseSalaryRequest& req) { int salfigure = req.getSalFigure(); if (salfigure > 5000) { //如果自己能处理,则自己处理 cout << req.getName() << "的加薪要求为:" << salfigure << "元,总经理审批通过!" << endl; } else { sendRequestToNextHandler(req); //自己不能处理,尝试找链中的下个对象来处理 } } }; } #endif /*****************************************************************/ /** * \file CTOApprove.h * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月7日 涂聚文 Geovin Du Visual Studio 2022 edit. * \author geovindu * \date June 2023 *********************************************************************/ #pragma once #ifndef CTOAPPROVER_H #define CTOAPPROVER_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 "RaiseSalaryRequest.h" #include "ParentSalaryApprover.h" using namespace std; namespace DuChainOfResponsibilityPattern { /// <summary> /// 技术总监子类 /// </summary> class CTOApprove : public ParentSalaryApprover { public : /// <summary> /// 处理加薪请求 /// </summary> /// <param name="req"></param> virtual void processRequest( const RaiseSalaryRequest& req) { int salfigure = req.getSalFigure(); if (salfigure > 1000 && salfigure <= 5000) { //如果自己能处理,则自己处理 cout << req.getName() << "的加薪要求为:" << salfigure << "元,技术总监审批通过!" << endl; } else { sendRequestToNextHandler(req); //自己不能处理,尝试找链中的下个对象来处理 } } }; } #endif /*****************************************************************/ /** * \file DepmentManagerApprove.h * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月7日 涂聚文 Geovin Du Visual Studio 2022 edit. * \author geovindu * \date June 2023 *********************************************************************/ #pragma once #ifndef DEPMENTMANAGERAPPROVER_H #define DEPMENTMANAGERAPPROVER_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 "RaiseSalaryRequest.h" #include "ParentSalaryApprover.h" using namespace std; namespace DuChainOfResponsibilityPattern { /// <summary> /// 部门经理子类 /// </summary> class DepmentManagerApprove: public ParentSalaryApprover { public : /// <summary> /// 处理加薪请求 /// </summary> /// <param name="req"></param> virtual void processRequest( const RaiseSalaryRequest& req) { int salfigure = req.getSalFigure(); if (salfigure <= 1000) { //如果自己能处理,则自己处理 cout << req.getName() << "的加薪要求为:" << salfigure << "元,部门经理审批通过!" << endl; } else { //自己不能处理,尝试找链中的下个对象来处理 sendRequestToNextHandler(req); } } }; } #endif /*****************************************************************/ /** * \file RaiseSalaryRequest.h * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月7日 涂聚文 Geovin Du Visual Studio 2022 edit. * \author geovindu * \date June 2023 *********************************************************************/ #pragma once #ifndef RAISESALARYREQUEST_H #define RAISESALARYREQUEST_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 DuChainOfResponsibilityPattern { /// <summary> /// 加薪请求类 /// </summary> class RaiseSalaryRequest { public : /// <summary> /// 构造函数 /// </summary> /// <param name="sname"></param> /// <param name="salfigure"></param> RaiseSalaryRequest( const string& sname, int salfigure) :mName(sname), misalFigure(salfigure) {} /// <summary> /// 获取请求加薪的人员名字 /// </summary> /// <returns></returns> const string& getName() const { return mName; } /// <summary> /// 获取请求加薪的数字 /// </summary> /// <returns></returns> int getSalFigure() const { return misalFigure; } private : /// <summary> /// 请求加薪的人员名字 /// </summary> string mName; /// <summary> /// 请求加薪的数字 /// </summary> int misalFigure; }; } #endif /*****************************************************************/ /** * \file ParentWordFilter.h * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月7日 涂聚文 Geovin Du Visual Studio 2022 edit. * \author geovindu * \date June 2023 *********************************************************************/ #pragma once #ifndef PARENTWORDFILTER_H #define PARENTWORDFILTER_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 DuChainOfResponsibilityPattern { /// <summary> /// /// </summary> class ParentWordFilter { public : /// <summary> /// /// </summary> ParentWordFilter() :nextChain( nullptr ) {} /// <summary> /// 做父类时析构函数应该为虚函数 /// </summary> virtual ~ParentWordFilter() {} /// <summary> /// 设置指向的职责链中的下个过滤器 /// </summary> /// <param name="next"></param> void setNextChain(ParentWordFilter* next) { nextChain = next; } /// <summary> /// 处理敏感词过滤请求 /// </summary> /// <param name="strWord"></param> /// <returns></returns> virtual string processRequest(string strWord) = 0; protected : /// <summary> /// 找链中的下个对象并把请求投递给下个链中的对象 /// </summary> /// <param name="strWord"></param> /// <returns></returns> string sendRequestToNextHandler(string strWord) { //找链中的下个对象 if (nextChain != nullptr ) { //把请求投递给链中的下个对象 return nextChain->processRequest(strWord); } return strWord; } private : /// <summary> /// /// </summary> ParentWordFilter* nextChain; }; } #endif /*****************************************************************/ /** * \file SexyWordFilter.h * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月7日 涂聚文 Geovin Du Visual Studio 2022 edit. * \author geovindu * \date June 2023 *********************************************************************/ #pragma once #ifndef SEXWORDFILTER_H #define SEXWORDFILTER_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 "ParentWordFilter.h" using namespace std; namespace DuChainOfResponsibilityPattern { /// <summary> /// /// </summary> class SexyWordFilter: public ParentWordFilter { public : /// <summary> /// /// </summary> /// <param name="strWord"></param> /// <returns></returns> virtual string processRequest(string strWord) { cout << "通过与词库比对,在strWord中查找\"性\"敏感词并用XXX来替换!" << endl; strWord += "XXX" ; //测试代码,具体的实现逻辑略...... return sendRequestToNextHandler(strWord); } }; } #endif /*****************************************************************/ /** * \file DirtyWordFilter.h * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月7日 涂聚文 Geovin Du Visual Studio 2022 edit. * \author geovindu * \date June 2023 *********************************************************************/ #pragma once #ifndef DIRTYWORDFILTER_H #define DIRTYWORDFILTER_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 "ParentWordFilter.h" using namespace std; namespace DuChainOfResponsibilityPattern { /// <summary> /// 脏话词过滤器子类 /// </summary> class DirtyWordFilter : public ParentWordFilter { public : virtual string processRequest(string strWord) { cout << "通过与词库比对,在strWord中查找\"脏话\"敏感词并用YYY来替换!" << endl; strWord += "YYY" ; return sendRequestToNextHandler(strWord); } }; } #endif /*****************************************************************/ /** * \file PoliticsWordFilter.h * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月7日 涂聚文 Geovin Du Visual Studio 2022 edit. * \author geovindu * \date June 2023 *********************************************************************/ #pragma once #ifndef POLITICSWORDFILTER_H #define POLITICSWORDFILTER_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 "ParentWordFilter.h" using namespace std; namespace DuChainOfResponsibilityPattern { /// <summary> /// 政治敏感词过滤器子类 /// </summary> class PoliticsWordFilter : public ParentWordFilter { public : virtual string processRequest(string strWord) { cout << "通过与词库比对,在strWord中查找\"政治\"敏感词并用ZZZ来替换!" << endl; strWord += "ZZZ" ; return sendRequestToNextHandler(strWord); } }; } #endif /*****************************************************************/ /** * \file GeovinDu.h * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月6日 涂聚文 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 <map> #include "PayrollProcessing.h" #include "RaiseSalaryRequest.h" #include "ParentSalaryApprover.h" #include "DepmentManagerApprove.h" #include "CTOApprove.h" #include "GeneralManagerApprove.h" #include "ParentWordFilter.h" #include "DirtyWordFilter.h" #include "SexyWordFilter.h" #include "PoliticsWordFilter.h" using namespace std; namespace DuJewelryChainOfResponsibilityPattern { /// <summary> /// /// </summary> class GeovinDu { private : public : /// <summary> /// /// </summary> void displaySimple(); /// <summary> /// /// </summary> void displayDuSimple(); /// <summary> /// /// </summary> void displayWordSimple(); }; } #endif /*****************************************************************/ /** * \file GeovinDu.cpp * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月7日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社 * \author geovindu * \date June 2023 *********************************************************************/ #include "GeovinDu.h" using namespace std; using namespace DuChainOfResponsibilityPattern; namespace DuJewelryChainOfResponsibilityPattern { /// <summary> /// /// </summary> void GeovinDu::displaySimple() { DuJewelryChainOfResponsibilityPattern::PayrollProcessing sh; sh.raiseSalaryRequest( "张三" , 15000); //张三要求加薪1.5万 sh.raiseSalaryRequest( "李四" , 3500); //李四要求加薪3千5 sh.raiseSalaryRequest( "王二" , 800); //王二要求加薪8百 } /// <summary> /// /// </summary> void GeovinDu::displayDuSimple() { //(1)创建出职责链中包含的各个对象(部门经理、技术总监、总经理) DuChainOfResponsibilityPattern::ParentSalaryApprover* pzzlinkobj1 = new DuChainOfResponsibilityPattern::DepmentManagerApprove(); DuChainOfResponsibilityPattern::ParentSalaryApprover* pzzlinkobj2 = new DuChainOfResponsibilityPattern::CTOApprove(); DuChainOfResponsibilityPattern::ParentSalaryApprover* pzzlinkobj3 = new DuChainOfResponsibilityPattern::GeneralManagerApprove(); //(2)将这些对象串在一起构成职责链(链表),现在职责链中pzzlinkobj1排在最前面,pzzlinkobj3排在最后面 pzzlinkobj1->setNextChain(pzzlinkobj2); pzzlinkobj2->setNextChain(pzzlinkobj3); pzzlinkobj3->setNextChain( nullptr ); //可以不写此行,因为ParSalApprover构造函数中设置了m_nextChain为nullptr //(3)创建几位员工关于加薪的请求(对象) DuChainOfResponsibilityPattern::RaiseSalaryRequest emp1Req( "张三" , 15000); //张三要求加薪1.5万 DuChainOfResponsibilityPattern::RaiseSalaryRequest emp2Req( "李四" , 3500); //李四要求加薪3千5 DuChainOfResponsibilityPattern::RaiseSalaryRequest emp3Req( "王二" , 800); //王二要求加薪8百 //看看每位员工的加薪请求由职责链中的哪个对象(部门经理、技术总监、总经理)来处理,从职责链中排在最前面的接收者(pzzlinkobj1)开始 pzzlinkobj1->processRequest(emp1Req); pzzlinkobj1->processRequest(emp2Req); pzzlinkobj1->processRequest(emp3Req); //(4)释放资源 delete pzzlinkobj1; delete pzzlinkobj2; delete pzzlinkobj3; } /// <summary> /// /// </summary> void GeovinDu::displayWordSimple() { //(1)创建出职责链中包含的各个对象(性敏感词过滤器、脏话词过滤器、政治敏感词过滤器) DuChainOfResponsibilityPattern::ParentWordFilter* pwflinkobj1 = new DuChainOfResponsibilityPattern::SexyWordFilter(); DuChainOfResponsibilityPattern::ParentWordFilter* pwflinkobj2 = new DuChainOfResponsibilityPattern::DirtyWordFilter(); DuChainOfResponsibilityPattern::ParentWordFilter* pwflinkobj3 = new DuChainOfResponsibilityPattern::PoliticsWordFilter(); //(2)将这些对象串在一起构成职责链(链表),现在职责链中pwflinkobj1排在最前面,pwflinkobj3排在最后面 pwflinkobj1->setNextChain(pwflinkobj2); pwflinkobj2->setNextChain(pwflinkobj3); pwflinkobj3->setNextChain( nullptr ); string strWordFilterResult = pwflinkobj1->processRequest( "你好,这里是过滤敏感词测试范例" ); //从职责链中排在最前面的接收者(pwflinkobj1)开始,processRequest的参数代表的是聊天内容 cout << "对敏感词过滤后的结果为:" << strWordFilterResult << endl; //(3)释放资源 delete pwflinkobj1; delete pwflinkobj2; delete pwflinkobj3; } } |
调用:
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 | /*****************************************************************/ /** * \file ConsoleDuChainOfResponsibilityPattern.cpp * \brief 责任链模式Chain Of Responsibility Pattern 亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility C++ 14 行为模式 * 2023年6月6日 涂聚文 Geovin Du Visual Studio 2022 edit.文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社 * \author geovindu * \date June 2023 *********************************************************************/ // ConsoleDuChainOfResponsibilityPattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #define _UNICODE #include <iostream> #include "GeovinDu.h" using namespace std; using namespace DuChainOfResponsibilityPattern; using namespace DuJewelryChainOfResponsibilityPattern; int main() { std::cout << "Hello World!涂聚文 Geovin Du\n" ; GeovinDu geovin; geovin.displaySimple(); cout << "******************" << endl; geovin.displayDuSimple(); cout << "*****" << endl; geovin.displayWordSimple(); 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 | Hello World!涂聚文 Geovin Du 张三的加薪要求为:15000元,总经理审批通过! 李四的加薪要求为:3500元,技术总监审批通过! 王二的加薪要求为:800元,部门经理审批通过! ****************** 张三的加薪要求为:15000元,总经理审批通过! 李四的加薪要求为:3500元,技术总监审批通过! 王二的加薪要求为:800元,部门经理审批通过! ***** 通过与词库比对,在strWord中查找 "性" 敏感词并用XXX来替换! 通过与词库比对,在strWord中查找 "脏话" 敏感词并用YYY来替换! 通过与词库比对,在strWord中查找 "政治" 敏感词并用ZZZ来替换! 对敏感词过滤后的结果为:你好,这里是过滤敏感词测试范例XXXYYYZZZ 请按任意键继续. . . |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Cpp programming
标签:
desgin patterns
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2015-06-07 csharp: Linq keyword example