【1】什么是命令模式?
命令模式:
【2】命令模式的代码示例:
代码示例:
1 #if 0 2 3 #include4 #include 5 using namespace std; 6 7 /* 8 * 运算基类 9 */ 10 class Operation 11 { 12 public: 13 double numberA; 14 double numberB; 15 public: 16 virtual double getResult() 17 { 18 return 0; 19 } 20 }; 21 22 /* 23 * 加法运算子类 24 */ 25 class addOperation : public Operation 26 { 27 double getResult() 28 { 29 return numberA + numberB; 30 } 31 }; 32 33 /* 34 * 减法运算子类 35 */ 36 class subOperation : public Operation 37 { 38 double getResult() 39 { 40 return numberA - numberB; 41 } 42 }; 43 44 /* 45 * 乘法运算子类 46 */ 47 class mulOperation : public Operation 48 { 49 double getResult() 50 { 51 return numberA * numberB; 52 } 53 }; 54 55 /* 56 * 除法运算子类 57 */ 58 class divOperation : public Operation 59 { 60 double getResult() 61 { 62 return numberA / numberB; 63 } 64 }; 65 66 /* 67 * 简单构建工厂 68 */ 69 class operFactory 70 { 71 public: 72 static Operation *createOperation(char c) 73 { 74 switch (c) 75 { 76 case '+': 77 return new addOperation; 78 break; 79 case '-': 80 return new subOperation; 81 break; 82 case '*': 83 return new mulOperation; 84 break; 85 case '/': 86 return new divOperation; 87 break; 88 default: 89 break; 90 } 91 } 92 }; 93 94 /* 95 * 客户端应用示例 96 */ 97 void main() 98 { 99 Operation *oper = operFactory::createOperation('+'); 100 oper->numberA = 9; 101 oper->numberB = 99; 102 cout << oper->getResult() << endl; 103 } 104 105 /* 106 * 简单工厂模式应用示例 107 */ 108 #include 109 #include 110 #include 111 using namespace std; 112 113 class CashSuper 114 { 115 public: 116 virtual double acceptMoney(double money) = 0; 117 }; 118 119 class CashNormal : public CashSuper 120 { 121 public: 122 double acceptMoney(double money) 123 { 124 return money; 125 } 126 }; 127 128 class CashRebate : public CashSuper 129 { 130 private: 131 double discount; 132 public: 133 CashRebate(double dis) 134 { 135 discount = dis; 136 } 137 double acceptMoney(double money) 138 { 139 return money * discount; 140 } 141 }; 142 143 144 class CashReturn : public CashSuper 145 { 146 private: 147 double moneyCondition; 148 double moneyReturn; 149 public: 150 CashReturn(double mc, double mr) 151 { 152 moneyCondition = mc; 153 moneyReturn = mr; 154 } 155 double acceptMoney(double money) 156 { 157 double result = money; 158 if (money >= moneyCondition) 159 { 160 result = money - floor(money / moneyCondition) * moneyReturn; 161 } 162 return result; 163 } 164 }; 165 166 class CashFactory 167 { 168 public: 169 static CashSuper *createCashAccept(string str) 170 { 171 CashSuper *cs = NULL; 172 if ("正常收费" == str) 173 { 174 return new CashNormal(); 175 } 176 else if ("打9折" == str) 177 { 178 return new CashRebate(0.9); 179 } 180 else if ("满300返200" == str) 181 { 182 return new CashReturn(300, 200); 183 } 184 return cs; 185 } 186 }; 187 188 189 void main() 190 { 191 CashSuper *cs = NULL; 192 193 cs = CashFactory::createCashAccept("打9折"); 194 cout << cs->acceptMoney(1000) << endl; 195 196 cs = CashFactory::createCashAccept("正常收费"); 197 cout << cs->acceptMoney(1000) << endl; 198 } 199 200 201 202 203 /* 204 * 策略模式 205 */ 206 207 #include 208 #include 209 #include 210 using namespace std; 211 212 class CashSuper 213 { 214 public: 215 virtual double acceptMoney(double money) = 0; 216 }; 217 218 class CashNormal : public CashSuper 219 { 220 public: 221 double acceptMoney(double money) 222 { 223 return money; 224 } 225 }; 226 227 class CashRebate : public CashSuper 228 { 229 private: 230 double discount; 231 232 public: 233 CashRebate(double dis) 234 { 235 discount = dis; 236 } 237 double acceptMoney(double money) 238 { 239 return money * discount; 240 } 241 }; 242 243 244 class CashReturn : public CashSuper 245 { 246 private: 247 double moneyCondition; 248 double moneyReturn; 249 250 public: 251 CashReturn(double mc, double mr) 252 { 253 moneyCondition = mc; 254 moneyReturn = mr; 255 } 256 double acceptMoney(double money) 257 { 258 double result = money; 259 if (money >= moneyCondition) 260 { 261 result = money - floor(money / moneyCondition) * moneyReturn; 262 } 263 return result; 264 } 265 }; 266 267 class CashContext 268 { 269 private: 270 CashSuper *cs; 271 public: 272 CashContext(CashSuper *cs) 273 { 274 this->cs = cs; 275 } 276 double getResult(double money) 277 { 278 return cs->acceptMoney(money); 279 } 280 }; 281 282 void main() 283 { 284 CashSuper *cs; 285 CashContext *cc; 286 double money = 1000; 287 288 cs = new CashRebate(0.8); 289 cc = new CashContext(cs); 290 cout << cc->getResult(money) << endl; 291 292 money = 1000; 293 cs = new CashNormal(); 294 cc = new CashContext(cs); 295 cout << cc->getResult(money) << endl; 296 297 } 298 299 300 301 /* 302 * 策略与工厂模式 303 */ 304 #include 305 #include 306 #include 307 using namespace std; 308 309 class CashSuper 310 { 311 public: 312 virtual double acceptMoney(double money) = 0; 313 }; 314 315 class CashNormal : public CashSuper 316 { 317 public: 318 double acceptMoney(double money) 319 { 320 return money; 321 } 322 }; 323 324 class CashRebate : public CashSuper 325 { 326 private: 327 double discount; 328 public: 329 CashRebate(double dis) 330 { 331 discount = dis; 332 } 333 double acceptMoney(double money) 334 { 335 return money * discount; 336 } 337 }; 338 339 340 class CashReturn : public CashSuper 341 { 342 private: 343 double moneyCondition; 344 double moneyReturn; 345 public: 346 CashReturn(double mc, double mr) 347 { 348 moneyCondition = mc; 349 moneyReturn = mr; 350 } 351 double acceptMoney(double money) 352 { 353 double result = money; 354 if (money >= moneyCondition) 355 { 356 result = money - floor(money / moneyCondition) * moneyReturn; 357 } 358 return result; 359 } 360 }; 361 362 class CashContext 363 { 364 private: 365 CashSuper *cs; 366 public: 367 CashContext(string str) 368 { 369 if ("正常收费" == str) 370 { 371 cs = new CashNormal(); 372 } 373 else if ("打9折" == str) 374 { 375 cs = new CashRebate(0.9); 376 } 377 else if ("满300送200" == str) 378 { 379 cs = new CashReturn(300, 200); 380 } 381 } 382 double getResult(double money) 383 { 384 return cs->acceptMoney(money); 385 } 386 }; 387 388 389 int main() 390 { 391 double money = 1000; 392 CashContext *cc = new CashContext("打9折"); 393 cout << cc->getResult(money); 394 return 0; 395 } 396 397 398 #include 399 #include 400 using namespace std; 401 402 class Person 403 { 404 private: 405 string m_strName; 406 public: 407 Person(string strName) 408 { 409 m_strName = strName; 410 } 411 412 Person(){} 413 414 virtual void show() 415 { 416 cout << "装扮的是:" << m_strName << endl; 417 } 418 }; 419 420 class Finery : public Person 421 { 422 protected: 423 Person *m_component; 424 public: 425 void decorate(Person* component) 426 { 427 m_component = component; 428 } 429 virtual void show() 430 { 431 m_component->show(); 432 } 433 }; 434 435 class TShirts : public Finery 436 { 437 public: 438 virtual void show() 439 { 440 m_component->show(); 441 cout << "T shirts" << endl; 442 } 443 }; 444 445 class BigTrouser : public Finery 446 { 447 public: 448 virtual void show() 449 { 450 m_component->show(); 451 cout << "Big Trouser" << endl; 452 } 453 }; 454 455 int main() 456 { 457 Person *p = new Person("小李"); 458 BigTrouser *bt = new BigTrouser(); 459 TShirts *ts = new TShirts(); 460 461 bt->decorate(p); 462 ts->decorate(bt); 463 ts->show(); 464 465 return 0; 466 } 467 468 #include 469 #include 470 using namespace std; 471 472 class SchoolGirl 473 { 474 public: 475 string name; 476 }; 477 478 /* 479 * 接口 480 */ 481 class IGiveGift 482 { 483 public: 484 virtual void giveDolls() = 0; 485 virtual void giveFlowers() = 0; 486 }; 487 488 /* 489 * 委托类 490 */ 491 class Pursuit : public IGiveGift 492 { 493 private: 494 SchoolGirl mm; 495 496 public: 497 Pursuit(SchoolGirl m) 498 { 499 mm = m; 500 } 501 void giveDolls() 502 { 503 cout << mm.name << " 送你娃娃" << endl; 504 } 505 void giveFlowers() 506 { 507 cout << mm.name << " 送你鲜花" << endl; 508 } 509 }; 510 511 /* 512 * 代理类 513 */ 514 class Proxy : public IGiveGift 515 { 516 private: 517 Pursuit gg; 518 519 public: 520 Proxy(SchoolGirl mm) : gg(mm) 521 { 522 } 523 void giveDolls() 524 { 525 gg.giveDolls(); 526 } 527 void giveFlowers() 528 { 529 gg.giveFlowers(); 530 } 531 }; 532 533 /* 534 * 客户端 535 */ 536 int main() 537 { 538 SchoolGirl lijiaojiao; 539 lijiaojiao.name = "李娇娇"; 540 Pursuit zhuojiayi(lijiaojiao); 541 Proxy daili(lijiaojiao); 542 543 daili.giveDolls(); 544 daili.giveFlowers(); 545 546 return 0; 547 } 548 549 #include 550 #include 551 using namespace std; 552 553 class Operation 554 { 555 public: 556 double numberA; 557 double numberB; 558 559 virtual double getResult() 560 { 561 return 0; 562 } 563 }; 564 565 class addOperation : public Operation 566 { 567 double getResult() 568 { 569 return numberA + numberB; 570 } 571 }; 572 573 574 class subOperation : public Operation 575 { 576 double getResult() 577 { 578 return numberA - numberB; 579 } 580 }; 581 582 class mulOperation : public Operation 583 { 584 double getResult() 585 { 586 return numberA * numberB; 587 } 588 }; 589 590 class divOperation : public Operation 591 { 592 double getResult() 593 { 594 return numberA / numberB; 595 } 596 }; 597 598 class IFactory 599 { 600 public: 601 virtual Operation *createOperation() = 0; 602 }; 603 604 class AddFactory : public IFactory 605 { 606 public: 607 static Operation *createOperation() 608 { 609 return new addOperation(); 610 } 611 }; 612 613 614 class SubFactory : public IFactory 615 { 616 public: 617 static Operation *createOperation() 618 { 619 return new subOperation(); 620 } 621 }; 622 623 class MulFactory : public IFactory 624 { 625 public: 626 static Operation *createOperation() 627 { 628 return new mulOperation(); 629 } 630 }; 631 632 class DivFactory : public IFactory 633 { 634 public: 635 static Operation *createOperation() 636 { 637 return new divOperation(); 638 } 639 }; 640 641 int main() 642 { 643 Operation *oper = MulFactory::createOperation(); 644 oper->numberA = 9; 645 oper->numberB = 99; 646 cout << oper->getResult() << endl; 647 return 0; 648 } 649 650 #include 651 #include 652 using namespace std; 653 654 class Prototype 655 { 656 private: 657 string str; 658 public: 659 Prototype(string s) 660 { 661 str = s; 662 } 663 Prototype() 664 { 665 str = ""; 666 } 667 void show() 668 { 669 cout << str << endl; 670 } 671 virtual Prototype *clone() = 0; 672 }; 673 674 class ConcretePrototype1 : public Prototype 675 { 676 public: 677 ConcretePrototype1(string s) : Prototype(s) 678 {} 679 ConcretePrototype1(){} 680 virtual Prototype *clone() 681 { 682 ConcretePrototype1 *p = new ConcretePrototype1(); 683 *p = *this; 684 return p; 685 } 686 }; 687 688 689 class ConcretePrototype2 : public Prototype 690 { 691 public: 692 ConcretePrototype2(string s) : Prototype(s) 693 {} 694 ConcretePrototype2(){} 695 virtual Prototype *clone() 696 { 697 ConcretePrototype2 *p = new ConcretePrototype2(); 698 *p = *this; 699 return p; 700 } 701 }; 702 703 int main() 704 { 705 ConcretePrototype1 *test = new ConcretePrototype1("小李"); 706 ConcretePrototype2 *test2 = (ConcretePrototype2 *)test->clone(); 707 test->show(); 708 test2->show(); 709 return 0; 710 } 711 712 #include 713 #include 714 using namespace std; 715 716 class Resume 717 { 718 private: 719 string name, sex, age, timeArea, company; 720 public: 721 Resume(string s) 722 { 723 name = s; 724 } 725 void setPersonalInfo(string s, string a) 726 { 727 sex = s; 728 age = a; 729 } 730 void setWorkExperience(string t, string c) 731 { 732 timeArea = t; 733 company = c; 734 } 735 void display() 736 { 737 cout << name << " " << sex << " " << age << endl; 738 cout << "工作经历: " << timeArea << " " << company << endl; 739 740 } 741 Resume *clone() 742 { 743 Resume *b = new Resume(name); 744 b->setPersonalInfo(sex, age); 745 b->setWorkExperience(timeArea, company); 746 return b; 747 } 748 }; 749 750 751 int main() 752 { 753 Resume *r = new Resume("李俊宏"); 754 r->setPersonalInfo("男","26"); 755 r->setWorkExperience("2007-2010","读研究生"); 756 r->display(); 757 758 759 Resume *r2 = r->clone(); 760 r2->setWorkExperience("2003-2007","读本科"); 761 762 r->display(); 763 r2->display(); 764 765 return 0; 766 } 767 768 #include 769 #include 770 using namespace std; 771 772 class TestPaper 773 { 774 public: 775 void question1() 776 { 777 cout << "1+1=" << answer1() << endl; 778 } 779 void question2() 780 { 781 cout << "1*1=" << answer2() << endl; 782 } 783 virtual string answer1() 784 { 785 return ""; 786 } 787 virtual string answer2() 788 { 789 return ""; 790 } 791 virtual ~TestPaper() 792 { 793 } 794 }; 795 796 class TestPaperA : public TestPaper 797 { 798 public: 799 string answer1() 800 { 801 return "2"; 802 } 803 virtual string answer2() 804 { 805 return "1"; 806 } 807 }; 808 809 class TestPaperB : public TestPaper 810 { 811 public: 812 string answer1() 813 { 814 return "3"; 815 } 816 virtual string answer2() 817 { 818 return "4"; 819 } 820 }; 821 822 823 int main() 824 { 825 cout << "A的试卷:" << endl; 826 TestPaper *s1 = new TestPaperA(); 827 s1->question1(); 828 s1->question2(); 829 delete s1; 830 831 cout << endl; 832 cout << "B的试卷:" << endl; 833 TestPaper *s2 = new TestPaperB(); 834 s2->question1(); 835 s2->question2(); 836 837 return 0; 838 } 839 840 #include 841 #include 842 #include 843 using namespace std; 844 845 class AbstractClass 846 { 847 public: 848 void Show() 849 { 850 cout << "我是" << GetName() << endl; 851 } 852 protected: 853 virtual string GetName() = 0; 854 }; 855 856 class Naruto : public AbstractClass 857 { 858 protected: 859 virtual string GetName() 860 { 861 return "火影史上最帅的六代目---一鸣惊人naruto"; 862 } 863 }; 864 865 class OnePice : public AbstractClass 866 { 867 protected: 868 virtual string GetName() 869 { 870 return "我是无恶不做的大海贼---路飞"; 871 } 872 }; 873 874 //客户端 875 int main() 876 { 877 Naruto* man = new Naruto(); 878 man->Show(); 879 880 OnePice* man2 = new OnePice(); 881 man2->Show(); 882 883 return 0; 884 } 885 886 #include 887 #include 888 using namespace std; 889 890 class Sub1 891 { 892 public: 893 void f1() 894 { 895 cout << "子系统的方法 1" << endl; 896 } 897 }; 898 899 class Sub2 900 { 901 public: 902 void f2() 903 { 904 cout << "子系统的方法 2" << endl; 905 } 906 }; 907 908 class Sub3 909 { 910 public: 911 void f3() 912 { 913 cout << "子系统的方法 3" << endl; 914 } 915 }; 916 917 class Facade 918 { 919 private: 920 Sub1 *s1; 921 Sub2 *s2; 922 Sub3 *s3; 923 public: 924 Facade() 925 { 926 s1 = new Sub1(); 927 s2 = new Sub2(); 928 s3 = new Sub3(); 929 } 930 931 void method() 932 { 933 s1->f1(); 934 s2->f2(); 935 s3->f3(); 936 } 937 938 ~Facade() 939 { 940 if (s1) 941 delete s1; 942 if (s2) 943 delete s2; 944 if (s3) 945 delete s3; 946 } 947 }; 948 949 int main() 950 { 951 Facade *f = new Facade(); 952 f->method(); 953 return 0; 954 } 955 956 #include 957 #include 958 #include 959 using namespace std; 960 961 class Person 962 { 963 public: 964 virtual void createHead() = 0; 965 virtual void createHand() = 0; 966 virtual void createBody() = 0; 967 virtual void createFoot() = 0; 968 }; 969 970 class ThinPerson : public Person 971 { 972 void createHead() 973 { 974 cout << "thin head" << endl; 975 } 976 void createHand() 977 { 978 cout << "thin hand" << endl; 979 } 980 void createBody() 981 { 982 cout << "thin body" << endl; 983 } 984 void createFoot() 985 { 986 cout << "thin foot" << endl; 987 } 988 }; 989 990 class FatPerson : public Person 991 { 992 void createHead() 993 { 994 cout << "fat head" << endl; 995 } 996 void createHand() 997 { 998 cout << "fat hand" << endl; 999 }1000 void createBody()1001 {1002 cout << "fat body" << endl;1003 }1004 void createFoot()1005 {1006 cout << "fat foot" << endl;1007 }1008 };1009 1010 1011 class Director1012 {1013 private:1014 Person *p;1015 public:1016 Director(Person *temp)1017 {1018 p = temp;1019 }1020 void create()1021 {1022 p->createHead();1023 p->createHand();1024 p->createBody();1025 p->createFoot();1026 }1027 };1028 1029 //客户端代码:1030 int main()1031 {1032 Person *p = new FatPerson();1033 Person *b = new ThinPerson();1034 Director *d = new Director(p);1035 Director *s = new Director(b);1036 d->create();1037 s->create();1038 delete d;1039 delete p;1040 delete s;1041 delete b;1042 1043 return 0;1044 }1045 1046 #include 1047 #include 1048 #include 1049 using namespace std;1050 1051 class Product1052 {1053 private:1054 vector product;1055 public:1056 void add(string str)1057 {1058 product.push_back(str);1059 }1060 void show()1061 {1062 vector ::iterator iter = product.begin();1063 while (iter != product.end())1064 {1065 cout << *iter << " ";1066 ++iter;1067 }1068 cout << endl;1069 }1070 };1071 1072 class Builder1073 {1074 public:1075 virtual void builderA() = 0;1076 virtual void builderB() = 0;1077 virtual Product *getResult() = 0;1078 };1079 1080 class ConcreteBuilder1 : public Builder1081 {1082 private:1083 Product *product;1084 public:1085 ConcreteBuilder1()1086 {1087 product = new Product();1088 }1089 virtual void builderA()1090 {1091 product->add("one");1092 }1093 virtual void builderB()1094 {1095 product->add("two");1096 }1097 virtual Product *getResult()1098 {1099 return product;1100 }1101 };1102 1103 1104 class ConcreteBuilder2 : public Builder1105 {1106 private:1107 Product *product;1108 public:1109 ConcreteBuilder2()1110 {1111 product = new Product();1112 }1113 virtual void builderA()1114 {1115 product->add("A");1116 }1117 virtual void builderB()1118 {1119 product->add("B");1120 }1121 virtual Product *getResult()1122 {1123 return product;1124 }1125 };1126 1127 class Director1128 {1129 private:1130 Product *p;1131 public:1132 void construct(Builder *bd)1133 {1134 bd->builderA();1135 bd->builderB();1136 p = bd->getResult();1137 }1138 Product *getResult()1139 {1140 return p;1141 }1142 };1143 1144 int main()1145 {1146 Director *director = new Director();1147 1148 Builder *bd1 = new ConcreteBuilder1();1149 director->construct(bd1);1150 Product *pbd1 = director->getResult();1151 1152 pbd1->show();1153 1154 return 0;1155 }1156 1157 // 观察者模式1158 1159 #include 1160 #include 1161 #include 1162 using namespace std;1163 1164 class Observer;1165 1166 class Subject1167 {1168 protected:1169 list
observers;1170 public:1171 string action;1172 public:1173 virtual void attach(Observer*) = 0;1174 virtual void detach(Observer*) = 0;1175 virtual void notify() = 0;1176 };1177 1178 class Observer1179 {1180 protected:1181 string name;1182 Subject *sub;1183 public:1184 Observer(string name, Subject *sub)1185 {1186 this->name = name;1187 this->sub = sub;1188 }1189 string getName()1190 {1191 return name;1192 }1193 virtual void update() = 0;1194 };1195 1196 class StockObserver : public Observer1197 {1198 public:1199 StockObserver(string name, Subject *sub) : Observer(name, sub)1200 {1201 }1202 void update();1203 };1204 1205 void StockObserver::update()1206 {1207 cout << name << " 收到消息:" << sub->action << endl;1208 if (sub->action == "梁所长来了!")1209 {1210 cout << "我马上关闭股票,装做很认真工作的样子!" << endl;1211 }1212 }1213 1214 class NBAObserver : public Observer1215 {1216 public:1217 NBAObserver(string name, Subject *sub) : Observer(name, sub)1218 {1219 }1220 void update();1221 };1222 1223 void NBAObserver::update()1224 {1225 cout << name << " 收到消息:" << sub->action << endl;1226 if (sub->action == "梁所长来了!")1227 {1228 cout << "我马上关闭NBA,装做很认真工作的样子!" << endl;1229 }1230 }1231 1232 class Secretary : public Subject1233 {1234 void attach(Observer *observer)1235 {1236 observers.push_back(observer);1237 }1238 void detach(Observer *observer)1239 {1240 list ::iterator iter = observers.begin();1241 while (iter != observers.end())1242 {1243 if ((*iter) == observer)1244 {1245 cout << "erase:" << observer->getName() << endl;1246 observers.erase(iter++);1247 }1248 else1249 {1250 ++iter;1251 }1252 }1253 }1254 void notify()1255 {1256 list ::iterator iter = observers.begin();1257 while (iter != observers.end())1258 {1259 (*iter)->update();1260 ++iter;1261 }1262 }1263 };1264 1265 1266 int main()1267 {1268 Subject *dwq = new Secretary();1269 1270 Observer *xs = new NBAObserver("xiaoshuai", dwq);1271 Observer *zy = new NBAObserver("zouyue", dwq);1272 Observer *lm = new StockObserver("limin", dwq);1273 1274 dwq->attach(xs);1275 dwq->attach(zy);1276 dwq->attach(lm);1277 1278 dwq->action = "去吃饭了!";1279 dwq->notify();1280 cout << endl;1281 1282 dwq->action = "梁所长来了!";1283 dwq->notify();1284 1285 dwq->detach(lm);1286 dwq->detach(zy);1287 dwq->detach(xs);1288 1289 return 0;1290 }1291 1292 #include 1293 #include 1294 using namespace std;1295 1296 class IUser1297 {1298 public:1299 virtual void getUser() = 0;1300 virtual void setUser() = 0;1301 };1302 1303 class SqlUser : public IUser1304 {1305 public:1306 void getUser()1307 {1308 cout << "在sql中返回user" << endl;1309 }1310 void setUser()1311 {1312 cout << "在sql中设置user" << endl;1313 }1314 };1315 1316 class AccessUser : public IUser1317 {1318 public:1319 void getUser()1320 {1321 cout << "在Access中返回user" << endl;1322 }1323 void setUser()1324 {1325 cout << "在Access中设置user" << endl;1326 }1327 };1328 1329 class IDepartment1330 {1331 public:1332 virtual void getDepartment() = 0;1333 virtual void setDepartment() = 0;1334 };1335 1336 class SqlDepartment : public IDepartment1337 {1338 public:1339 void getDepartment()1340 {1341 cout << "在sql中返回Department" << endl;1342 }1343 void setDepartment()1344 {1345 cout << "在sql中设置Department" << endl;1346 }1347 };1348 1349 class AccessDepartment : public IDepartment1350 {1351 public:1352 void getDepartment()1353 {1354 cout << "在Access中返回Department" << endl;1355 }1356 void setDepartment()1357 {1358 cout << "在Access中设置Department" << endl;1359 }1360 };1361 1362 class IFactory1363 {1364 public:1365 virtual IUser *createUser() = 0;1366 virtual IDepartment *createDepartment() = 0;1367 };1368 1369 class SqlFactory : public IFactory1370 {1371 public:1372 IUser *createUser() 1373 {1374 return new SqlUser();1375 }1376 IDepartment *createDepartment() 1377 {1378 return new SqlDepartment();1379 }1380 };1381 1382 class AccessFactory : public IFactory1383 {1384 public:1385 IUser *createUser()1386 {1387 return new AccessUser();1388 }1389 IDepartment *createDepartment() 1390 {1391 return new AccessDepartment();1392 }1393 };1394 1395 /*************************************************************/1396 1397 class DataAccess1398 {1399 private:1400 static string db;1401 public:1402 static IUser *createUser()1403 {1404 if (db == "access")1405 {1406 return new AccessUser();1407 }1408 else if (db == "sql")1409 {1410 return new SqlUser();1411 }1412 }1413 static IDepartment *createDepartment()1414 {1415 if (db == "access")1416 {1417 return new AccessDepartment();1418 }1419 else if (db == "sql")1420 {1421 return new SqlDepartment();1422 } 1423 }1424 };1425 1426 string DataAccess::db = "sql";1427 1428 /*************************************************************/1429 1430 int main()1431 {1432 IFactory *factory;1433 IUser *user;1434 IDepartment *department;1435 1436 factory = new AccessFactory();1437 user = factory->createUser();1438 department = factory->createDepartment();1439 1440 user->getUser();1441 user->setUser();1442 department->getDepartment();1443 department->setDepartment();1444 1445 user = DataAccess::createUser();1446 department = DataAccess::createDepartment();1447 1448 user->getUser();1449 user->setUser();1450 department->getDepartment();1451 department->setDepartment();1452 1453 return 0;1454 }1455 1456 #include 1457 #include 1458 using namespace std;1459 1460 class Work;1461 class State;1462 class ForenonnState;1463 1464 1465 class State1466 {1467 public:1468 virtual void writeProgram(Work*) = 0;1469 };1470 1471 class Work1472 {1473 public:1474 int hour;1475 State *current;1476 Work();1477 void writeProgram()1478 {1479 current->writeProgram(this);1480 }1481 };1482 1483 class EveningState : public State1484 {1485 public:1486 void writeProgram(Work *w)1487 {1488 cout << "当前时间: " << w->hour << "心情很好,在看《明朝那些事儿》,收获很大!" << endl;1489 }1490 };1491 1492 class AfternoonState : public State1493 {1494 public:1495 void writeProgram(Work *w)1496 {1497 if (w->hour < 19)1498 {1499 cout << "当前时间: " << w->hour << "下午午睡后,工作还是精神百倍!" << endl;1500 }1501 else1502 {1503 w->current = new EveningState();1504 w->writeProgram();1505 }1506 }1507 };1508 1509 class ForenonnState : public State1510 {1511 public:1512 void writeProgram(Work *w)1513 {1514 if (w->hour < 12)1515 {1516 cout << "当前时间: " << w->hour << "上午工作精神百倍!" << endl;1517 }1518 else1519 {1520 w->current = new AfternoonState();1521 w->writeProgram();1522 }1523 }1524 };1525 1526 Work::Work()1527 {1528 current = new ForenonnState();1529 }1530 1531 int main()1532 {1533 Work *w = new Work();1534 w->hour = 21;1535 w->writeProgram();1536 return 0;1537 }1538 //Reuslt:1539 //当前时间: 21心情很好,在看《明朝那些事儿》,收获很大!1540 1541 #include 1542 #include 1543 using namespace std;1544 1545 class Adaptee1546 {1547 public:1548 virtual void myRequest()1549 {1550 cout << "实际上的接口" << endl;1551 }1552 };1553 1554 class Target1555 {1556 public:1557 virtual void request() = 0;1558 virtual ~Target(){}1559 };1560 1561 class Adapter : public Target1562 {1563 private:1564 Adaptee adaptee;1565 public:1566 void request()1567 {1568 adaptee.myRequest();1569 }1570 };1571 1572 int main()1573 {1574 Target *target = new Adapter();1575 target->request();1576 delete target;1577 return 0;1578 }1579 //Result:1580 //实际上的接口1581 1582 #include 1583 #include 1584 using namespace std;1585 1586 class Player1587 {1588 public:1589 string name;1590 Player(string name)1591 {1592 this->name = name;1593 }1594 virtual void attack() = 0;1595 virtual void defence() = 0;1596 }; 1597 1598 class Forwards : public Player1599 {1600 public:1601 Forwards(string name) : Player(name){}1602 void attack()1603 {1604 cout << name << "前锋进攻" << endl;1605 }1606 void defence()1607 {1608 cout << name << "前锋防守" << endl;1609 }1610 };1611 1612 class Center : public Player1613 {1614 public:1615 Center(string name) : Player(name){}1616 void attack()1617 {1618 cout << name << "中锋进攻" << endl;1619 }1620 void defence()1621 {1622 cout << name << "中锋防守" << endl;1623 }1624 };1625 1626 class Backwards : public Player1627 {1628 public:1629 Backwards(string name) : Player(name){}1630 void attack()1631 {1632 cout << name << "后卫进攻" << endl;1633 }1634 void defence()1635 {1636 cout << name << "后卫防守" << endl;1637 }1638 };1639 /*****************************************************************/1640 class ForeignCenter1641 {1642 public:1643 string name;1644 ForeignCenter(string name)1645 {1646 this->name = name;1647 }1648 void myAttack()1649 {1650 cout << name << "外籍中锋进攻" << endl;1651 }1652 void myDefence()1653 {1654 cout << name << "外籍中锋防守" << endl;1655 }1656 };1657 /*****************************************************************/1658 class Translator : public Player1659 {1660 private:1661 ForeignCenter *fc;1662 public:1663 Translator(string name) : Player(name)1664 {1665 fc = new ForeignCenter(name); 1666 }1667 void attack()1668 {1669 fc->myAttack();1670 }1671 void defence()1672 {1673 fc->myDefence();1674 }1675 };1676 /*****************************************************************/1677 int main()1678 {1679 Player *p1 = new Center("李俊宏");1680 p1->attack();1681 p1->defence();1682 1683 Translator *tl = new Translator("姚明");1684 tl->attack();1685 tl->defence();1686 1687 return 0;1688 }1689 //Result:1690 /*1691 李俊宏中锋进攻1692 李俊宏中锋防守1693 姚明外籍中锋进攻1694 姚明外籍中锋防守1695 */1696 1697 1698 #include 1699 #include 1700 #include 1701 using namespace std;1702 1703 class Memo1704 {1705 public:1706 string state;1707 Memo(string state)1708 {1709 this->state = state;1710 }1711 };1712 1713 class Originator1714 {1715 public:1716 string state;1717 void setMemo(Memo *memo)1718 {1719 state = memo->state;1720 }1721 Memo *createMemo()1722 {1723 return new Memo(state);1724 }1725 void show()1726 {1727 cout << state << endl;1728 }1729 }; 1730 1731 class Caretaker1732 {1733 public:1734 vector memo;1735 public:1736 void save(Memo *memo)1737 {1738 (this->memo).push_back(memo);1739 }1740 Memo *getState(int i)1741 {1742 return memo[i];1743 }1744 };1745 1746 int main()1747 {1748 Originator *og = new Originator();1749 Caretaker *ct = new Caretaker(); 1750 1751 og->state = "on";1752 og->show(); 1753 ct->save(og->createMemo());1754 1755 og->state = "off";1756 og->show();1757 ct->save(og->createMemo());1758 1759 og->state = "middle";1760 og->show();1761 ct->save(og->createMemo());1762 1763 og->setMemo( ct->getState(1) );1764 og->show();1765 1766 return 0;1767 }1768 //Result:1769 /*1770 on1771 off1772 middle1773 off1774 */1775 1776 #include 1777 #include 1778 #include 1779 using namespace std;1780 1781 class Component1782 {1783 public:1784 string name;1785 Component(string name)1786 {1787 this->name = name;1788 }1789 virtual void add(Component *) = 0;1790 virtual void remove(Component *) = 0;1791 virtual void display(int) = 0;1792 };1793 1794 class Leaf : public Component1795 {1796 public:1797 Leaf(string name) : Component(name)1798 {}1799 void add(Component *c)1800 {1801 cout << "leaf cannot add" << endl;1802 }1803 void remove(Component *c)1804 {1805 cout << "leaf cannot remove" << endl;1806 }1807 void display(int depth)1808 {1809 string str(depth, '-');1810 str += name;1811 cout << str << endl;1812 }1813 };1814 1815 class Composite : public Component1816 {1817 private:1818 vector component;1819 public:1820 Composite(string name) : Component(name)1821 {}1822 void add(Component *c)1823 {1824 component.push_back(c);1825 }1826 void remove(Component *c)1827 {1828 vector ::iterator iter = component.begin();1829 while (iter != component.end())1830 {1831 if (*iter == c)1832 {1833 component.erase(iter++);1834 }1835 else1836 {1837 iter++;1838 }1839 }1840 }1841 void display(int depth)1842 {1843 string str(depth, '-');1844 str += name;1845 cout << str << endl;1846 1847 vector ::iterator iter=component.begin();1848 while (iter != component.end())1849 {1850 (*iter)->display(depth + 2);1851 iter++;1852 }1853 }1854 };1855 1856 1857 int main()1858 {1859 Component *p = new Composite("小李"); 1860 p->add(new Leaf("小王"));1861 p->add(new Leaf("小强"));1862 1863 Component *sub = new Composite("小虎"); 1864 sub->add(new Leaf("小王"));1865 sub->add(new Leaf("小明"));1866 sub->add(new Leaf("小柳"));1867 1868 p->add(sub);1869 p->display(0);1870 1871 cout << "*******" << endl;1872 sub->display(2);1873 1874 return 0;1875 }1876 //Result:1877 /*1878 小李1879 --小王1880 --小强1881 --小虎1882 ----小王1883 ----小明1884 ----小柳1885 *******1886 --小虎1887 ----小王1888 ----小明1889 ----小柳1890 */1891 1892 #include 1893 #include 1894 using namespace std;1895 1896 class Iterator;1897 1898 class Aggregate1899 {1900 public:1901 virtual Iterator *createIterator() = 0;1902 };1903 1904 class Iterator1905 {1906 public:1907 virtual void first() = 0;1908 virtual void next() = 0;1909 virtual bool isDone() = 0;1910 };1911 1912 class ConcreteAggregate : public Iterator1913 {1914 public:1915 void first()1916 {}1917 void next()1918 {}1919 bool isDone()1920 {}1921 };1922 1923 int main()1924 {1925 return 0;1926 }1927 1928 #include 1929 #include 1930 using namespace std;1931 1932 class Singleton1933 { 1934 private:1935 int i;1936 static Singleton *instance;1937 Singleton(int i)1938 {1939 this->i = i;1940 }1941 public:1942 static Singleton *getInstance()1943 {1944 return instance;1945 }1946 void show()1947 { 1948 cout << i << endl;1949 }1950 };1951 1952 Singleton* Singleton::instance = new Singleton(8899); 1953 1954 class A : public Singleton1955 {1956 1957 };1958 1959 int main()1960 {1961 Singleton *s = Singleton::getInstance();1962 Singleton *s2 = A::getInstance();1963 cout << s << endl;1964 cout << s2 << endl;1965 cout << (s == s2) << endl;1966 return 0;1967 }1968 1969 #include 1970 #include 1971 using namespace std;1972 1973 class HandsetSoft1974 {1975 public:1976 virtual void run() = 0;1977 };1978 1979 class HandsetGame : public HandsetSoft1980 {1981 public:1982 void run()1983 {1984 cout << "运行手机游戏" << endl;1985 }1986 };1987 1988 class HandsetAddressList : public HandsetSoft1989 {1990 public:1991 void run()1992 {1993 cout << "运行手机通讯录" << endl;1994 }1995 };1996 1997 class HandsetBrand1998 {1999 protected:2000 HandsetSoft *soft;2001 public:2002 void setHandsetSoft(HandsetSoft *soft)2003 {2004 this->soft = soft;2005 }2006 virtual void run() = 0;2007 };2008 2009 class HandsetBrandN : public HandsetBrand2010 {2011 public:2012 void run()2013 {2014 soft->run();2015 }2016 };2017 2018 class HandsetBrandM : public HandsetBrand2019 {2020 public:2021 void run()2022 {2023 soft->run();2024 }2025 };2026 2027 int main()2028 {2029 HandsetBrand *hb;2030 hb = new HandsetBrandM();2031 2032 hb->setHandsetSoft(new HandsetGame());2033 hb->run();2034 hb->setHandsetSoft(new HandsetAddressList());2035 hb->run();2036 2037 return 0;2038 }2039 #endif2040 2041 #include 2042 #include 2043 #include 2044 2045 using namespace std;2046 2047 class Barbecuer2048 {2049 public:2050 void bakeMutton()2051 {2052 cout << "烤羊肉串" << endl;2053 }2054 void bakeChickenWing()2055 {2056 cout << "烤鸡翅" << endl;2057 }2058 };2059 2060 class Command2061 {2062 protected:2063 Barbecuer *receiver;2064 public:2065 Command(Barbecuer *receiver)2066 {2067 this->receiver = receiver;2068 }2069 virtual void executeCommand() = 0;2070 };2071 2072 class BakeMuttonCommand : public Command2073 {2074 public:2075 BakeMuttonCommand(Barbecuer *receiver) : Command(receiver)2076 {}2077 void executeCommand()2078 {2079 receiver->bakeMutton();2080 }2081 };2082 2083 class BakeChikenWingCommand : public Command2084 {2085 public:2086 BakeChikenWingCommand(Barbecuer *receiver) : Command(receiver)2087 {}2088 void executeCommand()2089 {2090 receiver->bakeChickenWing();2091 }2092 };2093 2094 class Waiter2095 {2096 private:2097 Command *command;2098 public:2099 void setOrder(Command *command)2100 {2101 this->command = command;2102 }2103 void notify()2104 {2105 command->executeCommand();2106 }2107 };2108 2109 class Waiter22110 {2111 private:2112 list
orders;2113 public:2114 void setOrder(Command *command)2115 {2116 orders.push_back(command);2117 }2118 void cancelOrder(Command *command) 2119 {}2120 void notify()2121 {2122 list ::iterator iter = orders.begin();2123 while (iter != orders.end())2124 {2125 (*iter)->executeCommand();2126 iter++;2127 }2128 }2129 };2130 2131 2132 int main()2133 {2134 Barbecuer *boy = new Barbecuer();2135 Command *bm1 = new BakeMuttonCommand(boy);2136 Command *bm2 = new BakeMuttonCommand(boy);2137 Command *bc1 = new BakeChikenWingCommand(boy);2138 2139 cout << "Waiter2:" << endl;2140 Waiter2 *girl2 = new Waiter2();2141 2142 girl2->setOrder(bm1);2143 girl2->setOrder(bc1);2144 girl2->setOrder(bm2);2145 2146 girl2->notify();2147 2148 cout << "Waiter:" << endl;2149 Waiter *girl = new Waiter();2150 2151 girl->setOrder(bm1);2152 girl->notify();2153 2154 girl->setOrder(bm2);2155 girl->notify();2156 2157 girl->setOrder(bc1);2158 girl->notify();2159 2160 return 0;2161 }2162 //Result:2163 /*2164 Waiter2:2165 烤羊肉串2166 烤鸡翅2167 烤羊肉串2168 Waiter:2169 烤羊肉串2170 烤羊肉串2171 烤鸡翅2172 */
Good Good Study, Day Day Up.
顺序 选择 循环 总结