1 using System; 2 using System.CodeDom; 3 using System.Collections; 4 using System.Collections.Generic; 5 using System.Data.SqlClient; 6 using System.Linq; 7 using System.Runtime.Remoting; 8 using System.Runtime.Remoting.Contexts; 9 using System.Security.Policy; 10 using System.Text; 11 using System.Threading.Tasks; 12 13 namespace dhsjmsStudy 14 { 15 #region 简单工厂模式 16 public class operation 17 { 18 public decimal numberA { get; set; } 19 public decimal numberB { get; set; } 20 21 public virtual decimal GetResult() 22 { 23 return 0; 24 } 25 } 26 27 28 public class Add : operation 29 { 30 public override decimal GetResult() 31 { 32 return numberA + numberB; 33 } 34 } 35 36 public class Sub : operation 37 { 38 public override decimal GetResult() 39 { 40 return numberA - numberB; 41 } 42 } 43 44 public class Mlu : operation 45 { 46 public override decimal GetResult() 47 { 48 return numberA / numberB; 49 } 50 } 51 52 public class Div : operation 53 { 54 public override decimal GetResult() 55 { 56 return numberA * numberB; 57 } 58 } 59 #endregion 60 61 #region 策略模式 62 /// 63 /// 抽象收银类 64 /// 65 public abstract class CashSuper 66 { 67 public abstract double acceptCash(double money); 68 } 69 /// 70 /// 原价 71 /// 72 public class CashNormal : CashSuper 73 { 74 public override double acceptCash(double money) 75 { 76 return money; 77 } 78 } 79 80 /// 81 /// 打折 82 /// 83 public class CashRebate : CashSuper 84 { 85 private double moneyRebate = 1d; 86 87 public CashRebate(string moneyRebate) 88 { 89 this.moneyRebate = double.Parse(moneyRebate); 90 } 91 92 public override double acceptCash(double money) 93 { 94 return money * moneyRebate; 95 } 96 } 97 98 /// 99 /// 返利 100 /// 101 public class CashReturn : CashSuper 102 { 103 private double moneyCondition = 0; 104 private double moneyReturn = 0; 105 106 public CashReturn(string moneyCondition, string moneyReturn) 107 { 108 this.moneyCondition = double.Parse(moneyCondition); 109 this.moneyReturn = double.Parse(moneyReturn); 110 } 111 112 public override double acceptCash(double money) 113 { 114 return money >= moneyCondition ? (money - Math.Floor(money / moneyCondition) * moneyReturn) : money; 115 } 116 } 117 118 119 /// 120 /// 策略模式(策略封装了变化) 121 /// 122 public class CashContext 123 { 124 private CashSuper cs; 125 public CashContext(CashSuper csuper) 126 { 127 cs = csuper; 128 } 129 130 public CashContext(string type) 131 { 132 switch (type) 133 { 134 case "正常消费": 135 cs = new CashNormal(); 136 break; 137 case "满300返100": 138 cs = new CashReturn("300", "100"); 139 break; 140 case "打八折": 141 cs = new CashRebate("0.8"); 142 break; 143 } 144 } 145 146 public double GetResult(double money) 147 { 148 return cs.acceptCash(money); 149 } 150 } 151 152 #endregion 153 154 #region 装饰模式 155 156 public class Person 157 { 158 public string name { get; set; } 159 160 public Person() 161 { 162 163 } 164 165 public Person(string name) 166 { 167 this.name = name; 168 } 169 170 public virtual void Show() 171 { 172 Console.WriteLine("装扮的{0}", name); 173 } 174 } 175 176 /// 177 /// 服饰抽象类 178 /// 179 internal class Finery : Person 180 { 181 protected Person component; 182 183 184 public override void Show() 185 { 186 if (component != null) 187 component.Show(); 188 } 189 190 public void Decorate(Person component) 191 { 192 this.component = component; 193 } 194 195 } 196 197 internal class Tshirts : Finery 198 { 199 public override void Show() 200 { 201 Console.WriteLine("大T桖"); 202 base.Show(); 203 } 204 } 205 206 internal class BigTrouser : Finery 207 { 208 public override void Show() 209 { 210 Console.WriteLine("垮裤"); 211 base.Show(); 212 } 213 } 214 //其余... 215 #endregion 216 217 #region 代理模式 218 219 interface I代理 220 { 221 void SH(); 222 void WatchTv(); 223 } 224 225 public class ZQ : I代理 226 { 227 private BZQ person; 228 229 public ZQ(BZQ person) 230 { 231 this.person = person; 232 } 233 234 public void SH() 235 { 236 Console.WriteLine(person.Name + "送花"); 237 } 238 239 public void WatchTv() 240 { 241 Console.WriteLine(person.Name + "看电视"); 242 } 243 } 244 245 public class 代理人 : I代理 246 { 247 private ZQ gg; 248 249 public 代理人(BZQ mm) 250 { 251 gg = new ZQ(mm); 252 } 253 254 public void SH() 255 { 256 gg.SH(); 257 } 258 259 public void WatchTv() 260 { 261 gg.WatchTv(); 262 } 263 } 264 265 266 public class BZQ 267 { 268 public string Name { get; set; } 269 } 270 271 #endregion 272 273 #region 工厂方法模式 274 275 internal interface I工厂 276 { 277 operation CreateOperation(); 278 } 279 280 class AddFactory : I工厂 281 { 282 public operation CreateOperation() 283 { 284 return new Add(); 285 } 286 } 287 288 class SubFactory : I工厂 289 { 290 public operation CreateOperation() 291 { 292 return new Sub(); 293 } 294 } 295 296 class MulFactory : I工厂 297 { 298 public operation CreateOperation() 299 { 300 return new Mlu(); 301 } 302 } 303 304 class DivFactory : I工厂 305 { 306 public operation CreateOperation() 307 { 308 return new Div(); 309 } 310 } 311 312 /// 313 /// 雷锋 314 /// 315 316 internal class LeiFeng 317 { 318 public void Sweep() 319 { 320 Console.WriteLine("扫地"); 321 } 322 323 public void Wash() 324 { 325 Console.WriteLine("洗衣"); 326 } 327 328 public void BuyRice() 329 { 330 Console.WriteLine("买米"); 331 } 332 } 333 334 //学雷锋 335 internal class StudyPerson : LeiFeng 336 { 337 338 } 339 340 class Volunteer : LeiFeng 341 { 342 343 } 344 //简单雷锋工厂 345 internal class SimpleFactory 346 { 347 public static LeiFeng CreateLeiFeng(string type) 348 { 349 LeiFeng leiFeng = null; 350 switch (type) 351 { 352 case "大学生": 353 leiFeng = new StudyPerson(); 354 break; 355 case "社区志愿者": 356 leiFeng = new Volunteer(); 357 break; 358 } 359 return leiFeng; 360 } 361 } 362 363 interface IFactory 364 { 365 LeiFeng CreateLeiFeng(); 366 } 367 368 /// 369 /// 学雷锋的大学生工厂 370 /// 371 internal class UndergraduateFactory : IFactory 372 { 373 public LeiFeng CreateLeiFeng() 374 { 375 return new StudyPerson(); 376 } 377 } 378 379 internal class VolunteerFactory : IFactory 380 { 381 public LeiFeng CreateLeiFeng() 382 { 383 return new Volunteer(); 384 } 385 } 386 387 #endregion 388 389 #region 原型模式 390 /// 391 /// 原型类 392 /// 393 internal abstract class Prototype 394 { 395 private string id; 396 public string Id 397 { 398 get { return id; } 399 } 400 401 public Prototype(string id) 402 { 403 this.id = id; 404 } 405 406 public abstract Prototype Clone(); 407 } 408 409 /// 410 /// 具体原型类 411 /// 412 internal class ConcretePrototype : Prototype 413 { 414 public ConcretePrototype(string id) 415 : base(id) 416 { 417 418 } 419 420 public override Prototype Clone() 421 { 422 return (Prototype)this.MemberwiseClone(); 423 } 424 } 425 426 /// 427 /// 工作经历类 428 /// 429 internal class WorkExperience : ICloneable 430 { 431 public string wordDate { get; set; } 432 433 public string Company { get; set; } 434 435 public Object Clone() 436 { 437 return (Object)this.MemberwiseClone(); 438 } 439 } 440 /// 441 /// 简历类 442 /// 443 internal class Resume : ICloneable 444 { 445 private string name; 446 private string sex; 447 private string age; 448 //工作经历对象 449 private WorkExperience work; 450 451 public Resume(string name) 452 { 453 this.name = name; 454 work = new WorkExperience(); 455 } 456 457 private Resume(WorkExperience work) 458 { 459 this.work = (WorkExperience)work.Clone(); 460 } 461 462 //设置个人信息 463 public void SetPersonalInfo(string sex, string age) 464 { 465 this.sex = sex; 466 this.age = age; 467 } 468 469 //设置工作经历 470 public void SetWorkExperience(string wordDate, string company) 471 { 472 work.wordDate = wordDate; 473 work.Company = company; 474 } 475 476 //显示 477 public void Display() 478 { 479 Console.WriteLine("{0},{1},{2}", name, sex, age); 480 Console.WriteLine("工作经历:{0},{1}", work.wordDate, work.Company); 481 } 482 483 public Object Clone() 484 { 485 Resume obj = new Resume(this.work); 486 obj.name = this.name; 487 obj.sex = this.sex; 488 obj.age = this.age; 489 return obj; 490 } 491 } 492 493 #endregion 494 495 #region 模板方法模式 496 497 internal class TestPaper 498 { 499 public void TestQuestion1() 500 { 501 Console.WriteLine("第一题:a b c d "); 502 Console.WriteLine(Answer1()); 503 } 504 505 public void TestQuestion2() 506 { 507 Console.WriteLine("第二题:a b c d "); 508 Console.WriteLine(Answer2()); 509 } 510 511 public void TestQuestion3() 512 { 513 Console.WriteLine("第三题:a b c d "); 514 Console.WriteLine(Answer3()); 515 } 516 517 protected virtual string Answer1() 518 { 519 return ""; 520 } 521 protected virtual string Answer2() 522 { 523 return ""; 524 } 525 protected virtual string Answer3() 526 { 527 return ""; 528 } 529 } 530 531 internal class TestPaperA : TestPaper 532 { 533 protected override string Answer1() 534 { 535 return "a"; 536 } 537 protected override string Answer2() 538 { 539 return "b"; 540 } 541 protected override string Answer3() 542 { 543 return "c"; 544 } 545 546 } 547 548 549 internal class TestPaperB : TestPaper 550 { 551 protected override string Answer1() 552 { 553 return "c"; 554 } 555 protected override string Answer2() 556 { 557 return "b"; 558 } 559 protected override string Answer3() 560 { 561 return "a"; 562 } 563 } 564 565 #endregion 566 567 #region 外观模式 568 /// 569 /// 股票1 570 /// 571 internal class Stock1 572 { 573 public void Sell() 574 { 575 Console.WriteLine("1卖出"); 576 } 577 578 public void Buy() 579 { 580 Console.WriteLine("1买入"); 581 } 582 } 583 584 /// 585 /// 股票2 586 /// 587 internal class Stock2 588 { 589 public void Sell() 590 { 591 Console.WriteLine("2卖出"); 592 } 593 594 public void Buy() 595 { 596 Console.WriteLine("2买入"); 597 } 598 } 599 600 /// 601 /// 投资基金类 602 /// 603 internal class Fund 604 { 605 private Stock1 gu1; 606 private Stock2 gu2; 607 608 public Fund() 609 { 610 gu1 = new Stock1(); 611 gu2 = new Stock2(); 612 } 613 614 615 public void BuyFund() 616 { 617 gu1.Buy(); 618 gu2.Buy(); 619 } 620 621 public void SellFund() 622 { 623 gu1.Sell(); 624 gu2.Sell(); 625 } 626 627 628 } 629 630 #endregion 631 632 #region 建造者模式 633 634 public class Product 635 { 636 IList parts = new List (); 637 638 /// 639 /// 添加产品部件 640 /// 641 /// 642 public void Add(string part) 643 { 644 parts.Add(part); 645 } 646 647 /// 648 /// 列举所有的产品部件 649 /// 650 public void Show() 651 { 652 Console.WriteLine("产品 创建 ----"); 653 foreach (var part in parts) 654 { 655 Console.WriteLine(part); 656 } 657 } 658 } 659 660 /// 661 /// 抽象建造者类 662 /// 663 public abstract class Builder 664 { 665 public abstract void BuildePartA(); 666 667 public abstract void BuildePartB(); 668 669 public abstract Product GetResult(); 670 } 671 672 public class ConcreteBuilder1 : Builder 673 { 674 private Product product = new Product(); 675 676 public override void BuildePartA() 677 { 678 product.Add("部件a"); 679 } 680 681 public override void BuildePartB() 682 { 683 product.Add("部件b"); 684 } 685 686 public override Product GetResult() 687 { 688 return product; 689 } 690 } 691 692 public class ConcreteBuilder2 : Builder 693 { 694 private Product product = new Product(); 695 696 public override void BuildePartA() 697 { 698 product.Add("部件XX"); 699 } 700 701 public override void BuildePartB() 702 { 703 product.Add("部件YY"); 704 } 705 706 public override Product GetResult() 707 { 708 return product; 709 } 710 } 711 712 internal class Director 713 { 714 public void Construct(Builder builder) 715 { 716 builder.BuildePartA(); 717 builder.BuildePartB(); 718 } 719 } 720 721 #endregion 722 723 #region 观察者模式 724 725 #region 最终版 726 727 public delegate void EventHandler(); 728 /// 729 /// 通知者接口 730 /// 731 public interface ISubject 732 { 733 void Attach(Observer observer); 734 735 void Detach(Observer observer); 736 737 void Notify(); 738 739 string SubjectState { get; set; } 740 } 741 742 public class Boss : ISubject 743 { 744 private IList observers = new List (); 745 private string action; 746 747 public event EventHandler Update; 748 //老板状态 749 public string SubAction 750 { 751 get { return action; } 752 set { action = value; } 753 } 754 /// 755 /// 添加要帮忙的同事 756 /// 757 /// 758 public void Attach(Observer observer) 759 { 760 observers.Add(observer); 761 } 762 763 public void Detach(Observer observer) 764 { 765 observers.Remove(observer); 766 } 767 768 public void Notify1() 769 { 770 Update(); 771 } 772 //通知 773 public void Notify() 774 { 775 foreach (Observer o in observers) 776 { 777 o.Update(); 778 } 779 } 780 public string SubjectState { get; set; } 781 } 782 public abstract class Observer 783 { 784 protected string name; 785 protected ISubject sub; 786 787 public Observer(string name, ISubject sub) 788 { 789 this.name = name; 790 this.sub = sub; 791 } 792 793 public abstract void Update(); 794 } 795 796 /// 797 /// 看股票的 798 /// 799 public class StockObserver : Observer 800 { 801 public StockObserver(string name, ISubject sub) 802 : base(name, sub) 803 { 804 } 805 806 public override void Update() 807 { 808 Console.WriteLine("{0} {1} 关闭股票咯,继续工作。", sub.SubjectState, name); 809 } 810 811 //事件委托实现 812 public void CloseStockMarket() 813 { 814 Console.WriteLine("{0} {1} 关闭股票咯,继续工作。委托", sub.SubjectState, name); 815 } 816 } 817 818 819 /// 820 /// 看NBA的 821 /// 822 public class NBAObserver : Observer 823 { 824 public NBAObserver(string name, ISubject sub) 825 : base(name, sub) 826 { 827 } 828 829 public override void Update() 830 { 831 Console.WriteLine("{0} {1} 关闭NBA咯,继续工作。", sub.SubjectState, name); 832 } 833 834 835 //事件委托实现 836 public void CloseNBA() 837 { 838 Console.WriteLine("{0} {1} 关闭NBA咯,继续工作。委托", sub.SubjectState, name); 839 } 840 } 841 #endregion 842 843 844 845 public class Secretary 846 { 847 private IList observers = new List (); 848 private string action; 849 850 /// 851 /// 添加要帮忙的同事 852 /// 853 /// 854 public void Attach(StockObserver observer) 855 { 856 observers.Add(observer); 857 } 858 859 public void Detach(StockObserver observer) 860 { 861 observers.Remove(observer); 862 } 863 864 865 //通知 866 public void Notify() 867 { 868 foreach (StockObserver o in observers) 869 { 870 o.Update(); 871 } 872 } 873 874 //前台状态 875 public string SecretaryAction 876 { 877 get { return action; } 878 set { action = value; } 879 } 880 } 881 882 #endregion 883 884 #region 抽象工厂模式 885 886 public class User 887 { 888 public int id { get; set; } 889 890 public string name { get; set; } 891 892 } 893 894 public interface IUser 895 { 896 void Insert(User user); 897 898 User GetUser(int id); 899 } 900 901 public class SqlserverUser : IUser 902 { 903 public void Insert(User user) 904 { 905 Console.WriteLine("Sqlserver添加一条消息"); 906 } 907 908 public User GetUser(int id) 909 { 910 Console.WriteLine("Sqlserver查询一条消息"); 911 return null; 912 } 913 } 914 915 public class AccessUser : IUser 916 { 917 public void Insert(User user) 918 { 919 Console.WriteLine("Access添加一条消息"); 920 } 921 922 public User GetUser(int id) 923 { 924 Console.WriteLine("Access查询一条消息"); 925 return null; 926 } 927 } 928 929 public interface IFactoryDB 930 { 931 IUser CreateUser(); 932 } 933 934 public class SqlServerFactory : IFactoryDB 935 { 936 public IUser CreateUser() 937 { 938 return new SqlserverUser(); 939 } 940 } 941 942 public class AccessFactory : IFactoryDB 943 { 944 public IUser CreateUser() 945 { 946 return new AccessUser(); 947 } 948 } 949 950 //用简单工厂改进抽象工厂 951 //public class DataAccess 952 //{ 953 // private static readonly string db = "Sqlserver"; 954 // //private static readonly string db = "Access"; 955 956 // public static Iuser 957 //} 958 959 #endregion 960 961 #region 状态模式 962 963 public abstract class State 964 { 965 public abstract void Handle(Context context); 966 } 967 968 public class ConcreteStateA : State 969 { 970 public override void Handle(Context context) 971 { 972 context.State = new ConcreteStateB(); 973 } 974 } 975 976 public class ConcreteStateB : State 977 { 978 public override void Handle(Context context) 979 { 980 context.State = new ConcreteStateA(); 981 } 982 } 983 984 public class Context 985 { 986 private State state; 987 988 public Context(State state) 989 { 990 this.state = state; 991 } 992 993 public State State 994 { 995 get { return state; } 996 set 997 { 998 state = value; 999 Console.WriteLine("当前状态:" + state.GetType().Name);1000 }1001 }1002 1003 public void Request()1004 {1005 state.Handle(this);1006 }1007 }1008 1009 #endregion1010 1011 #region 适配器模式1012 1013 public class Target1014 {1015 public virtual void Request()1016 {1017 Console.WriteLine("普通请求!");1018 }1019 }1020 1021 public class Adaptee1022 {1023 public void SpecificRequest()1024 {1025 Console.WriteLine("特殊请求!");1026 }1027 }1028 1029 public class Adapter : Target1030 {1031 private Adaptee adaptee = new Adaptee();1032 1033 public override void Request()1034 {1035 adaptee.SpecificRequest();1036 }1037 }1038 1039 #endregion1040 1041 #region 备忘录模式1042 /// 1043 /// 发起人1044 /// 1045 public class Originator1046 {1047 public string state { get; set; }1048 1049 public Memento CreateMemento()1050 {1051 return new Memento(state);1052 }1053 1054 public void SetMemento(Memento memento)1055 {1056 state = memento.State;1057 }1058 1059 public void Show()1060 {1061 Console.WriteLine("State=" + state);1062 }1063 }1064 1065 /// 1066 /// 备忘类1067 /// 1068 public class Memento1069 {1070 private string state;1071 1072 public Memento(string state)1073 {1074 this.state = state;1075 }1076 1077 public string State1078 {1079 get { return state; }1080 }1081 }1082 /// 1083 /// 管理者1084 /// 1085 public class Caretaker1086 {1087 public Memento memento { get; set; }1088 }1089 1090 #endregion1091 1092 #region 组合模式1093 1094 public abstract class Component1095 {1096 protected string name;1097 1098 public Component(string name)1099 {1100 this.name = name;1101 }1102 1103 public abstract void Add(Component c);1104 public abstract void Remove(Component c);1105 public abstract void Display(int depth);1106 1107 }1108 1109 public class Leaf : Component1110 {1111 public Leaf(string name)1112 : base(name)1113 {1114 }1115 1116 public override void Add(Component c)1117 {1118 Console.WriteLine("添加");1119 }1120 1121 public override void Remove(Component c)1122 {1123 Console.WriteLine("删除");1124 }1125 1126 public override void Display(int depth)1127 {1128 Console.WriteLine(new string('-', depth) + name);1129 }1130 }1131 1132 public class Composite : Component1133 {1134 private List children = new List ();1135 public Composite(string name)1136 : base(name)1137 {1138 }1139 1140 public override void Add(Component c)1141 {1142 children.Add(c);1143 }1144 1145 public override void Remove(Component c)1146 {1147 children.Remove(c);1148 }1149 1150 public override void Display(int depth)1151 {1152 Console.WriteLine(new string('-', depth) + name);1153 1154 foreach (Component component in children)1155 {1156 component.Display(depth + 2);1157 }1158 }1159 }1160 1161 #endregion1162 1163 #region 迭代器模式1164 1165 /// 1166 /// Iterator迭代器抽象类1167 /// 1168 public abstract class Iterator1169 {1170 public abstract object First();1171 1172 public abstract object Next();1173 1174 public abstract bool IsDone();1175 public abstract object CurrentItem();1176 }1177 1178 /// 1179 /// 聚集抽象类1180 /// 1181 public abstract class Aggregate1182 {1183 public abstract Iterator CreateIterator();1184 }1185 1186 1187 public class ConcreteIterator : Iterator1188 {1189 private ConcreteAggregate aggregate;1190 private int current = 0;1191 1192 public ConcreteIterator(ConcreteAggregate aggregate)1193 {1194 this.aggregate = aggregate;1195 }1196 1197 public override object First()1198 {1199 return aggregate[0];1200 }1201 1202 public override object Next()1203 {1204 object ret = null;1205 current++;1206 if (current < aggregate.Count)1207 ret = aggregate[current];1208 return ret;1209 }1210 1211 public override bool IsDone()1212 {1213 return current >= aggregate.Count;1214 }1215 1216 public override object CurrentItem()1217 {1218 return aggregate[current];1219 }1220 }1221 1222 1223 public class ConcreteAggregate : Aggregate1224 {1225 private IList