• 【自学】大话设计模式控制台


       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     /// <summary>
      63     /// 抽象收银类
      64     /// </summary>
      65     public abstract class CashSuper
      66     {
      67         public abstract double acceptCash(double money);
      68     }
      69     /// <summary>
      70     /// 原价
      71     /// </summary>
      72     public class CashNormal : CashSuper
      73     {
      74         public override double acceptCash(double money)
      75         {
      76             return money;
      77         }
      78     }
      79 
      80     /// <summary>
      81     /// 打折
      82     /// </summary>
      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     /// <summary>
      99     /// 返利
     100     /// </summary>
     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     /// <summary>
     120     /// 策略模式(策略封装了变化)
     121     /// </summary>
     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     /// <summary>
     177     /// 服饰抽象类
     178     /// </summary>
     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     /// <summary>
     313     /// 雷锋
     314     /// </summary>
     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     /// <summary>
     369     /// 学雷锋的大学生工厂
     370     /// </summary>
     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     /// <summary>
     391     /// 原型类
     392     /// </summary>
     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     /// <summary>
     410     /// 具体原型类
     411     /// </summary>
     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     /// <summary>
     427     /// 工作经历类
     428     /// </summary>
     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     /// <summary>
     441     /// 简历类
     442     /// </summary>
     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     /// <summary>
     569     /// 股票1
     570     /// </summary>
     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     /// <summary>
     585     /// 股票2
     586     /// </summary>
     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     /// <summary>
     601     /// 投资基金类
     602     /// </summary>
     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<string> parts = new List<string>();
     637 
     638         /// <summary>
     639         /// 添加产品部件
     640         /// </summary>
     641         /// <param name="part"></param>
     642         public void Add(string part)
     643         {
     644             parts.Add(part);
     645         }
     646 
     647         /// <summary>
     648         /// 列举所有的产品部件
     649         /// </summary>
     650         public void Show()
     651         {
     652             Console.WriteLine("产品 创建 ----");
     653             foreach (var part in parts)
     654             {
     655                 Console.WriteLine(part);
     656             }
     657         }
     658     }
     659 
     660     /// <summary>
     661     /// 抽象建造者类
     662     /// </summary>
     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     /// <summary>
     729     /// 通知者接口
     730     /// </summary>
     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<Observer> observers = new List<Observer>();
     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         /// <summary>
     755         /// 添加要帮忙的同事
     756         /// </summary>
     757         /// <param name="observer"></param>
     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     /// <summary>
     797     /// 看股票的
     798     /// </summary>
     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     /// <summary>
     820     /// 看NBA的
     821     /// </summary>
     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<StockObserver> observers = new List<StockObserver>();
     848         private string action;
     849 
     850         /// <summary>
     851         /// 添加要帮忙的同事
     852         /// </summary>
     853         /// <param name="observer"></param>
     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     #endregion
    1010 
    1011     #region 适配器模式
    1012 
    1013     public class Target
    1014     {
    1015         public virtual void Request()
    1016         {
    1017             Console.WriteLine("普通请求!");
    1018         }
    1019     }
    1020 
    1021     public class Adaptee
    1022     {
    1023         public void SpecificRequest()
    1024         {
    1025             Console.WriteLine("特殊请求!");
    1026         }
    1027     }
    1028 
    1029     public class Adapter : Target
    1030     {
    1031         private Adaptee adaptee = new Adaptee();
    1032 
    1033         public override void Request()
    1034         {
    1035             adaptee.SpecificRequest();
    1036         }
    1037     }
    1038 
    1039     #endregion
    1040 
    1041     #region 备忘录模式
    1042     /// <summary>
    1043     /// 发起人
    1044     /// </summary>
    1045     public class Originator
    1046     {
    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     /// <summary>
    1066     /// 备忘类
    1067     /// </summary>
    1068     public class Memento
    1069     {
    1070         private string state;
    1071 
    1072         public Memento(string state)
    1073         {
    1074             this.state = state;
    1075         }
    1076 
    1077         public string State
    1078         {
    1079             get { return state; }
    1080         }
    1081     }
    1082     /// <summary>
    1083     /// 管理者
    1084     /// </summary>
    1085     public class Caretaker
    1086     {
    1087         public Memento memento { get; set; }
    1088     }
    1089 
    1090     #endregion
    1091 
    1092     #region 组合模式
    1093 
    1094     public abstract class Component
    1095     {
    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 : Component
    1110     {
    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 : Component
    1133     {
    1134         private List<Component> children = new List<Component>();
    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     #endregion
    1162 
    1163     #region 迭代器模式
    1164 
    1165     /// <summary>
    1166     /// Iterator迭代器抽象类
    1167     /// </summary>
    1168     public abstract class Iterator
    1169     {
    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     /// <summary>
    1179     /// 聚集抽象类
    1180     /// </summary>
    1181     public abstract class Aggregate
    1182     {
    1183         public abstract Iterator CreateIterator();
    1184     }
    1185 
    1186 
    1187     public class ConcreteIterator : Iterator
    1188     {
    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 : Aggregate
    1224     {
    1225         private IList<object> items = new List<object>();
    1226         public override Iterator CreateIterator()
    1227         {
    1228             return new ConcreteIterator(this);
    1229         }
    1230 
    1231         public int Count
    1232         {
    1233             get { return items.Count; }
    1234         }
    1235 
    1236         public object this[int index]
    1237         {
    1238             get { return items[index]; }
    1239             set { items.Insert(index, value); }
    1240         }
    1241     }
    1242 
    1243     #endregion
    1244 
    1245     #region 单例模式
    1246 
    1247     public class Singleton
    1248     {
    1249         public static Singleton instance;
    1250 
    1251         private static readonly object syncRoot = new object();
    1252         private Singleton()
    1253         {
    1254 
    1255         }
    1256 
    1257         public static Singleton GetInstance()
    1258         {
    1259             //双重锁定
    1260             if (instance == null)
    1261             {
    1262                 lock (syncRoot)
    1263                 {
    1264                     if (instance == null)
    1265                     {
    1266                         instance = new Singleton();
    1267                     }
    1268                 }
    1269             }
    1270             return instance;
    1271         }
    1272     }
    1273 
    1274     #endregion
    1275 
    1276     #region 桥接模式
    1277 
    1278     //public partial class HandsetGame
    1279     //{
    1280     //    public virtual void Run()
    1281     //    {
    1282 
    1283     //    }
    1284     //}
    1285 
    1286     //public class HandsetMGame : HandsetGame
    1287     //{
    1288     //    public override void Run()
    1289     //    {
    1290     //        Console.WriteLine("运行M");
    1291     //    }
    1292     //}
    1293 
    1294     //public class HandsetNGame : HandsetGame
    1295     //{
    1296     //    public override void Run()
    1297     //    {
    1298     //        Console.WriteLine("运行N");
    1299     //    }
    1300     //}
    1301 
    1302 
    1303     #region 松耦合的程序
    1304     /// <summary>
    1305     /// 手机软件
    1306     /// </summary>
    1307     public abstract class HandsetSoft
    1308     {
    1309         public abstract void Run();
    1310     }
    1311     /// <summary>
    1312     /// 手机游戏
    1313     /// </summary>
    1314     public class HandsetGame : HandsetSoft
    1315     {
    1316         public override void Run()
    1317         {
    1318             Console.WriteLine("运行手机游戏");
    1319         }
    1320     }
    1321     /// <summary>
    1322     /// 手机通讯录
    1323     /// </summary>
    1324     public class HandsetAddressList : HandsetSoft
    1325     {
    1326         public override void Run()
    1327         {
    1328             Console.WriteLine("运行手机通讯录");
    1329         }
    1330     }
    1331 
    1332     /// <summary>
    1333     /// 手机品牌类
    1334     /// </summary>
    1335     public abstract class HandsetBrand
    1336     {
    1337         protected HandsetSoft soft;
    1338 
    1339         public void SetHandsetSoft(HandsetSoft soft)
    1340         {
    1341             this.soft = soft;
    1342         }
    1343 
    1344         public abstract void Run();
    1345     }
    1346 
    1347     public class HandsetBrandM : HandsetBrand
    1348     {
    1349         public override void Run()
    1350         {
    1351             soft.Run();
    1352         }
    1353     }
    1354     public class HandsetBrandN : HandsetBrand
    1355     {
    1356         public override void Run()
    1357         {
    1358             soft.Run();
    1359         }
    1360     }
    1361     #endregion
    1362     #endregion
    1363 
    1364     #region 命令模式
    1365     /// <summary>
    1366     /// 抽象命令
    1367     /// </summary>
    1368     public abstract class Command
    1369     {
    1370         protected Barbecuer receiver;
    1371 
    1372         public Command(Barbecuer receiver)
    1373         {
    1374             this.receiver = receiver;
    1375         }
    1376 
    1377         public abstract void ExcuteCommand();
    1378     }
    1379 
    1380 
    1381     public class Barbecuer
    1382     {
    1383         public void BakeMutton()
    1384         {
    1385             Console.WriteLine("烤肉");
    1386         }
    1387 
    1388         public void BakeChickenWing()
    1389         {
    1390             Console.WriteLine("鸡翅");
    1391         }
    1392     }
    1393 
    1394     /// <summary>
    1395     /// 烤肉
    1396     /// </summary>
    1397     public class BakeMuttonCommand : Command
    1398     {
    1399         public BakeMuttonCommand(Barbecuer receiver)
    1400             : base(receiver)
    1401         {
    1402         }
    1403 
    1404         public override void ExcuteCommand()
    1405         {
    1406             receiver.BakeMutton();
    1407         }
    1408     }
    1409 
    1410     /// <summary>
    1411     /// 烤鸡翅
    1412     /// </summary>
    1413     public class BakeChickenWingCommand : Command
    1414     {
    1415         public BakeChickenWingCommand(Barbecuer receiver)
    1416             : base(receiver)
    1417         {
    1418         }
    1419 
    1420         public override void ExcuteCommand()
    1421         {
    1422             receiver.BakeChickenWing();
    1423         }
    1424     }
    1425 
    1426     /// <summary>
    1427     /// 服务员
    1428     /// </summary>
    1429     public class Waiter
    1430     {
    1431         private IList<Command> orders = new List<Command>();
    1432 
    1433         public void SetOrder(Command command)
    1434         {
    1435             if (command.ToString() == "dhsjmsStudy.BakeChickenWingCommand")
    1436             {
    1437                 Console.WriteLine("鸡翅卖完了!");
    1438             }
    1439             else
    1440             {
    1441                 orders.Add(command);
    1442                 Console.WriteLine("添加订单:" + command + " 时间:" + DateTime.Now);
    1443             }
    1444         }
    1445 
    1446         public void CancelOrder(Command command)
    1447         {
    1448             orders.Remove(command);
    1449             Console.WriteLine("取消订单:" + command + " 时间:" + DateTime.Now);
    1450         }
    1451 
    1452         public void Notify()
    1453         {
    1454             foreach (Command command in orders)
    1455             {
    1456                 command.ExcuteCommand();
    1457             }
    1458         }
    1459     }
    1460 
    1461     #endregion
    1462 
    1463     #region 责任链模式
    1464 
    1465     public class Request
    1466     {
    1467         public string requestType { get; set; }
    1468         public string requestContent { get; set; }
    1469         public int numBer { get; set; }
    1470     }
    1471 
    1472     /// <summary>
    1473     /// 管理者
    1474     /// </summary>
    1475     public abstract class Manager
    1476     {
    1477         protected string name;
    1478 
    1479         //管理者的上级
    1480         protected Manager superior;
    1481 
    1482         public Manager(string name)
    1483         {
    1484             this.name = name;
    1485         }
    1486 
    1487         //设置管理者的上级
    1488         public void SetSuperior(Manager superior)
    1489         {
    1490             this.superior = superior;
    1491         }
    1492 
    1493         public abstract void RequestApplications(Request request);
    1494     }
    1495 
    1496     /// <summary>
    1497     /// 经理
    1498     /// </summary>
    1499     public class CommonManager : Manager
    1500     {
    1501         public CommonManager(string name)
    1502             : base(name)
    1503         {
    1504         }
    1505 
    1506         public override void RequestApplications(Request request)
    1507         {
    1508             if (request.requestType == "请假" && request.numBer <= 2)
    1509             {
    1510                 Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.requestContent, request.numBer);
    1511             }
    1512             else
    1513             {
    1514                 if (null != superior)
    1515                     superior.RequestApplications(request);
    1516             }
    1517         }
    1518     }
    1519 
    1520     /// <summary>
    1521     /// 总监
    1522     /// </summary>
    1523     public class Majordomo : Manager
    1524     {
    1525         public Majordomo(string name)
    1526             : base(name)
    1527         {
    1528         }
    1529 
    1530         public override void RequestApplications(Request request)
    1531         {
    1532             if (request.requestType == "请假" && request.numBer <= 5)
    1533             {
    1534                 Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.requestContent, request.numBer);
    1535             }
    1536             else
    1537             {
    1538                 if (null != superior)
    1539                     superior.RequestApplications(request);
    1540             }
    1541         }
    1542     }
    1543 
    1544 
    1545     /// <summary>
    1546     /// 总经理
    1547     /// </summary>
    1548     public class GeneralManager : Manager
    1549     {
    1550         public GeneralManager(string name)
    1551             : base(name)
    1552         {
    1553         }
    1554 
    1555         public override void RequestApplications(Request request)
    1556         {
    1557             if (request.requestType == "请假")
    1558             {
    1559                 Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.requestContent, request.numBer);
    1560             }
    1561             else if (request.requestType == "加薪" && request.numBer <= 500)
    1562             {
    1563                 Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.requestContent, request.numBer);
    1564             }
    1565             else if (request.requestType == "加薪" && request.numBer > 500)
    1566             {
    1567                 Console.WriteLine("{0}:{1} 数量{2} 再说吧", name, request.requestContent, request.numBer);
    1568             }
    1569 
    1570         }
    1571     }
    1572     #endregion
    1573 
    1574     #region 中介者模式
    1575     /// <summary>
    1576     /// 抽象中介类
    1577     /// </summary>
    1578     public abstract class Mediator
    1579     {
    1580         public abstract void Send(string message, Colleague colleague);
    1581     }
    1582 
    1583     public class ConcreteMediator : Mediator
    1584     {
    1585         private ConcreteMediator1 col1;
    1586         private ConcreteMediator2 col2;
    1587 
    1588         public ConcreteMediator1 Col1
    1589         {
    1590             set { col1 = value; }
    1591         }
    1592 
    1593         public ConcreteMediator2 Col2
    1594         {
    1595             set { col2 = value; }
    1596         }
    1597         public override void Send(string message, Colleague colleague)
    1598         {
    1599             if (colleague == col1)
    1600             {
    1601                 col2.Notify(message);
    1602             }
    1603             else
    1604             {
    1605                 col1.Notify(message);
    1606             }
    1607         }
    1608     }
    1609 
    1610     /// <summary>
    1611     /// 抽象同事类
    1612     /// </summary>
    1613     public abstract class Colleague
    1614     {
    1615         protected Mediator mediator;
    1616 
    1617         public Colleague(Mediator mediator)
    1618         {
    1619             this.mediator = mediator;
    1620         }
    1621     }
    1622     public class ConcreteMediator1 : Colleague
    1623     {
    1624         public ConcreteMediator1(Mediator mediator)
    1625             : base(mediator)
    1626         {
    1627         }
    1628 
    1629         public void Send(string message)
    1630         {
    1631             mediator.Send(message, this);
    1632         }
    1633 
    1634         public void Notify(string message)
    1635         {
    1636             Console.WriteLine("同事1得到的消息:" + message);
    1637         }
    1638     }
    1639 
    1640     public class ConcreteMediator2 : Colleague
    1641     {
    1642         public ConcreteMediator2(Mediator mediator)
    1643             : base(mediator)
    1644         {
    1645         }
    1646 
    1647         public void Send(string message)
    1648         {
    1649             mediator.Send(message, this);
    1650         }
    1651 
    1652         public void Notify(string message)
    1653         {
    1654             Console.WriteLine("同事2得到的消息:" + message);
    1655         }
    1656     }
    1657     #endregion
    1658 
    1659     #region 享元模式
    1660 
    1661     public abstract class Flyweight
    1662     {
    1663         public abstract void Operation(int extrinsicstate);
    1664     }
    1665 
    1666     public class ConcreteFlyweight : Flyweight
    1667     {
    1668         public override void Operation(int extrinsicstate)
    1669         {
    1670             Console.WriteLine("具体Flyweight:" + extrinsicstate);
    1671         }
    1672     }
    1673 
    1674     public class UnshareConcreteFlyweight : Flyweight
    1675     {
    1676         public override void Operation(int extrinsicstate)
    1677         {
    1678             Console.WriteLine("不共享的具体Flyweight:" + extrinsicstate);
    1679         }
    1680     }
    1681 
    1682     public class FlyweightFactory
    1683     {
    1684         private Hashtable flyweights = new Hashtable();
    1685 
    1686         public FlyweightFactory()
    1687         {
    1688             flyweights.Add("X", new ConcreteFlyweight());
    1689             flyweights.Add("Y", new ConcreteFlyweight());
    1690             flyweights.Add("Z", new ConcreteFlyweight());
    1691         }
    1692 
    1693         public Flyweight GetFlyweight(string key)
    1694         {
    1695             return ((Flyweight)flyweights[key]);
    1696         }
    1697     }
    1698     #region 网站享元
    1699 
    1700     public abstract class WebSite
    1701     {
    1702         public abstract void Use();
    1703     }
    1704 
    1705     /// <summary>
    1706     /// 具体网站
    1707     /// </summary>
    1708     public class ConcreteWebSite : WebSite
    1709     {
    1710         private string _name;
    1711 
    1712         public ConcreteWebSite(string name)
    1713         {
    1714             this._name = name;
    1715         }
    1716 
    1717         public override void Use()
    1718         {
    1719             Console.WriteLine("网站分类:" + _name);
    1720         }
    1721     }
    1722 
    1723     /// <summary>
    1724     /// 网站工厂类
    1725     /// </summary>
    1726     public class WebSiteFactory
    1727     {
    1728         private Hashtable flyweights = new Hashtable();
    1729 
    1730         //获取网站分类
    1731         public WebSite GetWebSiteCategory(string key)
    1732         {
    1733             if (!flyweights.ContainsKey(key))
    1734                 flyweights.Add(key, new ConcreteWebSite(key));
    1735             return ((WebSite)flyweights[key]);
    1736         }
    1737 
    1738         public int GetWebSiteCount()
    1739         {
    1740             return flyweights.Count;
    1741         }
    1742     }
    1743 
    1744     #endregion
    1745     #endregion
    1746 
    1747     #region 解释器模式
    1748 
    1749     public class Context1
    1750     {
    1751         public string inPut { get; set; }
    1752 
    1753         public string outPut { get; set; }
    1754     }
    1755 
    1756     public class TerminalExpression : AbstractExpression
    1757     {
    1758         public override void Interpret(Context1 context)
    1759         {
    1760             Console.WriteLine("终端解释器");
    1761         }
    1762     }
    1763 
    1764     public abstract class AbstractExpression
    1765     {
    1766         public abstract void Interpret(Context1 context);
    1767     }
    1768 
    1769     public class NoterminalExpression : AbstractExpression
    1770     {
    1771         public override void Interpret(Context1 context)
    1772         {
    1773             Console.WriteLine("非终端解释器");
    1774         }
    1775     }
    1776 
    1777 
    1778     #endregion
    1779 
    1780     #region 访问者模式
    1781 
    1782     public abstract class Visitor
    1783     {
    1784         public abstract void VisitorConcreteElementA(ConcreteElementA A);
    1785         public abstract void VisitorConcreteElementB(ConcreteElementB B);
    1786     }
    1787 
    1788     public class ConcreteVisitorA : Visitor
    1789     {
    1790         public override void VisitorConcreteElementA(ConcreteElementA A)
    1791         {
    1792             Console.WriteLine("{0}被{1}访问", A.GetType().Name, this.GetType().Name);
    1793         }
    1794 
    1795         public override void VisitorConcreteElementB(ConcreteElementB B)
    1796         {
    1797             Console.WriteLine("{0}被{1}访问", B.GetType().Name, this.GetType().Name);
    1798         }
    1799     }
    1800 
    1801     public class ConcreteVisitorB : Visitor
    1802     {
    1803         public override void VisitorConcreteElementA(ConcreteElementA A)
    1804         {
    1805             Console.WriteLine("{0}被{1}访问", A.GetType().Name, this.GetType().Name);
    1806         }
    1807 
    1808         public override void VisitorConcreteElementB(ConcreteElementB B)
    1809         {
    1810             Console.WriteLine("{0}被{1}访问", B.GetType().Name, this.GetType().Name);
    1811         }
    1812     }
    1813 
    1814     public abstract class Element
    1815     {
    1816         public abstract void Accept(Visitor visitor);
    1817     }
    1818 
    1819     public class ConcreteElementA : Element
    1820     {
    1821         public override void Accept(Visitor visitor)
    1822         {
    1823             visitor.VisitorConcreteElementA(this);
    1824         }
    1825 
    1826         public void OperationA()
    1827         {
    1828 
    1829         }
    1830     }
    1831 
    1832     public class ConcreteElementB : Element
    1833     {
    1834         public override void Accept(Visitor visitor)
    1835         {
    1836             visitor.VisitorConcreteElementB(this);
    1837         }
    1838 
    1839         public void OperationB()
    1840         {
    1841 
    1842         }
    1843     }
    1844 
    1845     /// <summary>
    1846     /// 提供高层接口,允许访问者访问
    1847     /// </summary>
    1848     public class ObjectStructure
    1849     {
    1850         private IList<Element> elements = new List<Element>();
    1851 
    1852         public void Attach(Element element)
    1853         {
    1854             elements.Add(element);
    1855         }
    1856 
    1857         public void Detach(Element element)
    1858         {
    1859             elements.Remove(element);
    1860         }
    1861 
    1862         public void Accept(Visitor visitor)
    1863         {
    1864             foreach (Element e in elements)
    1865             {
    1866                 e.Accept(visitor);
    1867             }
    1868         }
    1869     }
    1870 
    1871     #endregion
    1872     class Program
    1873     {
    1874 
    1875         public static operation CreateOperate(string operate)
    1876         {
    1877             operation oper = null;
    1878             switch (operate)
    1879             {
    1880                 case "+":
    1881                     oper = new Add();
    1882                     break;
    1883                 case "-":
    1884                     oper = new Sub();
    1885                     break;
    1886                 case "*":
    1887                     oper = new Div();
    1888                     break;
    1889                 case "/":
    1890                     oper = new Mlu();
    1891                     break;
    1892             }
    1893             return oper;
    1894         }
    1895 
    1896         public static CashSuper CreateCashAccept(string type)
    1897         {
    1898             CashSuper cs = null;
    1899             switch (type)
    1900             {
    1901                 case "正常消费":
    1902                     cs = new CashNormal();
    1903                     break;
    1904                 case "满300返100":
    1905                     cs = new CashReturn("300", "100");
    1906                     break;
    1907                 case "打八折":
    1908                     cs = new CashRebate("0.8");
    1909                     break;
    1910             }
    1911             return cs;
    1912         }
    1913 
    1914         static void Main(string[] args)
    1915         {
    1916             #region 简单工厂模式
    1917             //operation oper = Program.CreateOperate("*");
    1918             //oper.numberB = 10;
    1919             //oper.numberA = 11;
    1920             //Console.WriteLine(oper.GetResult());
    1921             #endregion
    1922 
    1923             #region 策略模式与简单工厂组合
    1924             //CashContext cc = new CashContext("满300返100");
    1925             //double totalPrices = cc.GetResult(10000);
    1926             //Console.WriteLine(totalPrices);
    1927             #endregion
    1928 
    1929             #region 单一原则
    1930             //如果一个类承担的职责过多,就等于把这些职责耦合在一起。
    1931             //一个职责的变化可能会削弱或者抑制这个类完成其他职责的能力。
    1932             //这种耦合会导致脆弱的设计,当发生变化时,设计会遭受意想不到的破坏【ASD】。
    1933 
    1934             //软件设计真正要做的许多内容,就是发现职责并相互分离。
    1935             //如果你能够想到多于一个的动机去改变一个类,那么这个类就具备多于一个的职责,
    1936             //就应该考虑类的职责分离。
    1937             #endregion
    1938 
    1939             #region 开放-封闭原则
    1940             //(大话代码结构)将BLL的业务逻辑开放,将修改等数据处理封闭。
    1941 
    1942             //(大话设计模式)软件实体(类、模块、函数等等)应该可以扩展,但是不可修改。
    1943             //对于扩展是开放的,对于更改是封闭的。
    1944 
    1945             //对于需求、程序改动是通过增加新代码进行的,而不是更改现有代码。
    1946             #endregion
    1947 
    1948             #region 依赖倒转原则
    1949             //针对于接口编程,不要对实现编程。
    1950 
    1951             //A.高层模块不应该依赖底层模块。两个都依赖抽象。
    1952             //B.抽象不应依赖细节,细节应依赖抽象。
    1953             #endregion
    1954 
    1955             #region 里氏代换原则
    1956             //子类型必须能够替换它们的父类型。
    1957             #endregion
    1958 
    1959             #region 装饰模式
    1960             //动态地给一个对象添加一些额外的职能,
    1961             //就增加功能来说,装饰模式比生成子类更灵活
    1962 
    1963             //Person ly = new Person("蝼蚁");
    1964             //Console.WriteLine("第一种装扮:");
    1965             //Finery dtx = new Tshirts();
    1966             //Finery kk = new BigTrouser();
    1967             //dtx.Decorate(ly);
    1968             //kk.Decorate(dtx);
    1969             //kk.Show();
    1970             #endregion
    1971 
    1972             #region 代理模式
    1973             ////为其他对象提供一种代理以控制对这个对象的访问
    1974             //BZQ jiaojiao = new BZQ();
    1975             //jiaojiao.Name = "娇娇";
    1976 
    1977             //代理人 daili = new 代理人(jiaojiao);
    1978             //daili.SH();
    1979             //daili.WatchTv();
    1980             #endregion
    1981 
    1982             #region 工厂方法模式
    1983             //I工厂 operFactory = new AddFactory();
    1984             //operation oper = operFactory.CreateOperation();
    1985             //oper.numberA = 1;
    1986             //oper.numberB = 2;
    1987             //Console.WriteLine(oper.GetResult());
    1988 
    1989             ////大学生和志愿者可以通过实例化不同类实现。
    1990             ////IFactory factory= new UndergraduateFactory();
    1991             //IFactory factory = new VolunteerFactory();
    1992             //LeiFeng leifeng = factory.CreateLeiFeng();
    1993             //leifeng.BuyRice();
    1994             //leifeng.Sweep();
    1995             //leifeng.Wash();
    1996 
    1997             #endregion
    1998 
    1999             #region 原型模式
    2000             ////用原型实例指定创建对象的种类,
    2001             ////并通过拷贝这些原型创建新的对象。
    2002 
    2003             ////ConcretePrototype pl = new ConcretePrototype("I");
    2004             ////ConcretePrototype cl = (ConcretePrototype)pl.Clone();
    2005 
    2006             ////Console.WriteLine("Clone:{0}", cl.Id);
    2007             ////Clone复制结构不复制数据
    2008             ////Copy复制结构也复制数据
    2009 
    2010             //Resume a = new Resume("test");
    2011             //a.SetPersonalInfo("男", "22");
    2012             //a.SetWorkExperience("1995-2018", "xx公司");
    2013 
    2014             ////此处Clone()=>实则是在重新实例化一个Resume再赋值
    2015             //Resume b = (Resume)a.Clone();
    2016             //b.SetWorkExperience("1998-2006", "yy公司");
    2017 
    2018             //Resume c = (Resume)a.Clone();
    2019             //c.SetWorkExperience("1998-2006", "zz公司");
    2020 
    2021             //a.Display();
    2022             //b.Display();
    2023             //c.Display();
    2024             #endregion
    2025 
    2026             #region 模板方法模式
    2027             ////定义一个操作中的算法的骨架,
    2028             ////而将一些步骤延迟到子类中。
    2029             ////使得子类可以不改变一个算法的结构
    2030             ////即可重定义该算法的某些特定步骤。
    2031             //Console.WriteLine("考试A");
    2032             //TestPaper studentA = new TestPaperA();
    2033             //studentA.TestQuestion1();
    2034             //studentA.TestQuestion2();
    2035             //studentA.TestQuestion3();
    2036 
    2037             //Console.WriteLine("考试B");
    2038             //TestPaper studentB = new TestPaperB();
    2039             //studentB.TestQuestion1();
    2040             //studentB.TestQuestion2();
    2041             //studentB.TestQuestion3();
    2042 
    2043             #endregion
    2044 
    2045             #region 迪米特法则
    2046             //如果两个类不必彼此直接通信,
    2047             //那么这两个类就不应当发生直接的相互作用。
    2048             //如果其中一个类需要调用另一个类的某一个方法的话,
    2049             //可以通过第三者转发这个调用。
    2050 
    2051             //如果再公司电脑坏了,可以寻求该IT部门,而不是直接找某个人。
    2052             #endregion
    2053 
    2054             #region 外观模式
    2055             ////为子系统中的一组接口提供一个一致的界面,
    2056             ////此模式定义了一个高层接口,使得子系统更容易使用。
    2057             ////遇到复杂庞大的系统维护时,我们可添加一个简单的接口,
    2058             ////减少它们之间的依赖。
    2059 
    2060             ////Stock1 gu1 = new Stock1();
    2061             ////Stock2 gu2 = new Stock2();
    2062 
    2063             ////gu1.Buy();
    2064             ////gu2.Buy();
    2065 
    2066             ////gu1.Sell();
    2067             ////gu2.Sell();
    2068 
    2069             //Fund jijin = new Fund();
    2070             //jijin.BuyFund();
    2071             //jijin.SellFund();
    2072             #endregion
    2073 
    2074             #region 建造者模式
    2075             ////将一个复杂对象的构造与它的表示分离,
    2076             ////使得同样的构造过程可以创建不同的表示。
    2077 
    2078             //Director director = new Director();
    2079             //Builder b1 = new ConcreteBuilder1();
    2080             //Builder b2 = new ConcreteBuilder2();
    2081 
    2082             //director.Construct(b1);
    2083             //Product p1 = b1.GetResult();
    2084             //p1.Show();
    2085 
    2086             //director.Construct(b2);
    2087             //Product p2 = b2.GetResult();
    2088             //p2.Show();
    2089             #endregion
    2090 
    2091             #region 观察者模式
    2092             ////定义一种一对多的依赖关系,
    2093             ////让多个观察者对象同时监听某一个主题对象。
    2094             ////当这个主题对象变化时,会通知所有观察对象,
    2095             ////使它们能自动更新自己。
    2096 
    2097             //Secretary qiantai = new Secretary();
    2098             //StockObserver tongzhi1 = new StockObserver("通知人1", qiantai);
    2099             //StockObserver tongzhi2 = new StockObserver("通知人2", qiantai);
    2100 
    2101             //qiantai.Attach(tongzhi1);
    2102             //qiantai.Attach(tongzhi2);
    2103 
    2104             //qiantai.SecretaryAction = "老板来了!";
    2105             //qiantai.Notify();
    2106 
    2107             #region 最终版
    2108             //Boss huhansan = new Boss();
    2109 
    2110             //StockObserver tongzhi1 = new StockObserver("通知人1", huhansan);
    2111             //NBAObserver tongzhi2 = new NBAObserver("通知人2", huhansan);
    2112 
    2113             //huhansan.Attach(tongzhi1);
    2114             //huhansan.Attach(tongzhi2);
    2115 
    2116             //huhansan.Detach(tongzhi1);
    2117 
    2118             //huhansan.SubjectState = "我胡汉三又回来啦";
    2119 
    2120             //huhansan.Notify();
    2121             #endregion
    2122 
    2123             #region 事件委托
    2124             //Boss huhansan = new Boss();
    2125 
    2126             //StockObserver tongzhi1 = new StockObserver("通知人1", huhansan);
    2127             //NBAObserver tongzhi2 = new NBAObserver("通知人2", huhansan);
    2128             //huhansan.Update += new EventHandler(tongzhi1.CloseStockMarket);//应该是把tongzhi1对象给一起传入方法了。
    2129             //huhansan.Update += new EventHandler(tongzhi2.CloseNBA);
    2130             //huhansan.SubjectState = "我胡汉三又回来啦";
    2131             //huhansan.Notify1();
    2132             #endregion
    2133             #endregion
    2134 
    2135             #region 抽象工厂模式
    2136             //提供一个创建一系列相关或相互依赖对象的接口,
    2137             //而无需指定它们的具体类。
    2138 
    2139             //User user = new User();
    2140             //IFactoryDB factory = new SqlServerFactory();
    2141             //IUser iu = factory.CreateUser();
    2142             //iu.Insert(user);
    2143             //iu.GetUser(1);
    2144 
    2145             //用反射+抽象工厂的数据访问程序
    2146 
    2147             #endregion
    2148 
    2149             #region 状态模式
    2150             ////当一个对象的内在状态改变时允许改变其行为,
    2151             ////这个对象看起来像是改变了其类。
    2152 
    2153             //Context c = new Context(new ConcreteStateA());
    2154             //c.Request();
    2155             //c.Request();
    2156             //c.Request();
    2157             //c.Request();
    2158             #endregion
    2159 
    2160             #region 适配器模式
    2161             //将一个类的接口转换成客户希望的另一个接口,
    2162             //Adapter模式使得原本由于接口不兼容而不能一起工作的类,
    2163             //可以一起工作。
    2164 
    2165             //Target target = new Adapter();
    2166             //target.Request();
    2167             #endregion
    2168 
    2169             #region 备忘录模式
    2170             ////在不破坏封装性的前提下,
    2171             ////捕获一个对象的内部状态,
    2172             ////并在该对象之外保存这个状态。
    2173             //Originator o = new Originator();
    2174             //o.state = "On";
    2175             //o.Show();
    2176 
    2177             //Caretaker c = new Caretaker();
    2178             //c.memento = o.CreateMemento();
    2179 
    2180             //o.state = "off";
    2181             //o.Show();
    2182 
    2183             //o.SetMemento(c.memento);
    2184             //o.Show();
    2185 
    2186             #endregion
    2187 
    2188             #region 组合模式
    2189             ////将对象组合成树形结构以表示“部分-整体”的层次结构。
    2190             ////组合模式使得用户对单个对象和组合对象的使用具有一致性。
    2191 
    2192             ////生成根节点
    2193             //Composite root = new Composite("root");
    2194             //root.Add(new Leaf("Leaf A"));
    2195             //root.Add(new Leaf("Leaf B"));
    2196 
    2197             ////根上长出分支
    2198             //Composite comp = new Composite("Composite X");
    2199             //comp.Add(new Leaf("Leaf XA"));
    2200             //comp.Add(new Leaf("Leaf XB"));
    2201 
    2202             //root.Add(comp);
    2203 
    2204             //Composite comp2 = new Composite("Composite XY");
    2205             //comp.Add(new Leaf("Leaf XYA"));
    2206             //comp.Add(new Leaf("Leaf XYB"));
    2207 
    2208             //comp.Add(comp2);
    2209             //root.Add(new Leaf("Leaf C"));
    2210 
    2211             //Leaf leaf = new Leaf("Leaf D");
    2212             //root.Add(leaf);
    2213             //root.Remove(leaf);
    2214 
    2215             ////显示大树
    2216             //root.Display(1);
    2217             #endregion
    2218 
    2219             #region 迭代器模式
    2220             //ConcreteAggregate a = new ConcreteAggregate();
    2221             //for (int i = 0; i < 10; i++)
    2222             //{
    2223             //    a[i] = i;
    2224             //}
    2225 
    2226             //Iterator i1 = new ConcreteIterator(a);
    2227             //while (!i1.IsDone())
    2228             //{
    2229             //    Console.WriteLine("{0} 请买票!", i1.CurrentItem());
    2230             //    i1.Next();
    2231             //}
    2232             #endregion
    2233 
    2234             #region 单例模式
    2235             ////保证一个类仅有一个实例,
    2236             ////并提供一个访问它的全局访问点。
    2237 
    2238             //Singleton s1 = Singleton.GetInstance();
    2239             //Singleton s2 = Singleton.GetInstance();
    2240 
    2241             //if (s1 == s2)
    2242             //    Console.WriteLine("相同实例");
    2243 
    2244 
    2245             #endregion
    2246 
    2247             #region 桥接模式
    2248             //将抽象部分与它的实现部分分离,
    2249             //使它们都可以独立变化。
    2250 
    2251             //同样一个东西,运行在不同平台。
    2252             //只要把东西弄好,其他平台继承后实现父类即可。
    2253 
    2254             ////HandsetGame a;
    2255             ////a = new HandsetMGame();
    2256             ////a.Run();
    2257 
    2258             ////a= new HandsetNGame();
    2259             ////a.Run();
    2260 
    2261             //HandsetBrand hb;
    2262             //hb = new HandsetBrandM();
    2263 
    2264             //hb.SetHandsetSoft(new HandsetGame());
    2265             //hb.Run();
    2266 
    2267             //hb.SetHandsetSoft(new HandsetAddressList());
    2268             //hb.Run();
    2269 
    2270             //hb = new HandsetBrandN();
    2271 
    2272             //hb.SetHandsetSoft(new HandsetGame());
    2273             //hb.Run();
    2274 
    2275             //hb.SetHandsetSoft(new HandsetAddressList());
    2276             //hb.Run();
    2277 
    2278             #endregion
    2279 
    2280             #region 合成/聚合复用原则
    2281             //尽量使用合成/聚合,
    2282             //尽量不要使用类继承
    2283             #endregion
    2284 
    2285             #region 命令模式
    2286             ////将一个请求封装为一个对象,
    2287             ////从而使你可用不同的请求对客户进行参数化;
    2288             ////对请求排队或记录请求日志,以及支持可撤销的操作。
    2289             //Barbecuer boy = new Barbecuer();
    2290             //Command c1 = new BakeMuttonCommand(boy);
    2291             //Command c2 = new BakeMuttonCommand(boy);
    2292             //Command c3 = new BakeChickenWingCommand(boy);
    2293 
    2294             //Waiter girl = new Waiter();
    2295             //girl.SetOrder(c1);
    2296             //girl.SetOrder(c2);
    2297             //girl.SetOrder(c3);
    2298 
    2299             //girl.Notify();
    2300 
    2301             #endregion
    2302 
    2303             #region 责任链模式
    2304             ////使多个对象都有机会处理请求,
    2305             ////从而避免请求的发送者和接收者的耦合关系。
    2306             ////将这个对象连成一条链,并沿着这条链传递该请求,
    2307             ////直到有一个对象处理它为止。
    2308             //CommonManager jinli = new CommonManager("金立");
    2309             //Majordomo zongjian = new Majordomo("宗建");
    2310             //GeneralManager zongjingli = new GeneralManager("宗金丽");
    2311 
    2312             //jinli.SetSuperior(zongjian);
    2313             //zongjian.SetSuperior(zongjingli);
    2314 
    2315             //Request request = new Request();
    2316             //request.requestType = "请假";
    2317             //request.requestContent = "小菜请假";
    2318             //request.numBer = 1;
    2319             //jinli.RequestApplications(request);
    2320 
    2321             //Request request2 = new Request();
    2322             //request2.requestType = "请假";
    2323             //request2.requestContent = "小菜请假";
    2324             //request2.numBer = 1;
    2325             //jinli.RequestApplications(request2);
    2326 
    2327             //Request request3 = new Request();
    2328             //request3.requestType = "加薪";
    2329             //request3.requestContent = "小菜请求加薪";
    2330             //request3.numBer = 500;
    2331             //jinli.RequestApplications(request3);
    2332 
    2333             //Request request4 = new Request();
    2334             //request4.requestType = "加薪";
    2335             //request4.requestContent = "小菜请求加薪";
    2336             //request4.numBer = 1000;
    2337             //jinli.RequestApplications(request4);
    2338             #endregion
    2339 
    2340             #region 中介者模式
    2341             //用一个中介对象来封装一系列的对象交互,
    2342             //中介者使各对象不需要显式地互相调用,
    2343             //从而使其耦合松散,而且可以独立地改变它们之间的交互。
    2344 
    2345             ConcreteMediator m = new ConcreteMediator();
    2346             ConcreteMediator1 c1 = new ConcreteMediator1(m);
    2347             ConcreteMediator2 c2 = new ConcreteMediator2(m);
    2348 
    2349             m.Col1 = c1;
    2350             m.Col2 = c2;
    2351 
    2352             c1.Send("发送消息");
    2353             c2.Send("收到消息");
    2354 
    2355             #endregion
    2356 
    2357             #region 享元模式
    2358             //////运用共享技术有效地支持大量细粒度的对象。
    2359             ////int ext = 22;
    2360 
    2361             ////FlyweightFactory f = new FlyweightFactory();
    2362 
    2363             ////Flyweight fx = f.GetFlyweight("X");
    2364             ////fx.Operation(--ext);
    2365 
    2366             ////Flyweight fy = f.GetFlyweight("Y");
    2367             ////fy.Operation(--ext);
    2368 
    2369             ////Flyweight fz = f.GetFlyweight("Z");
    2370             ////fz.Operation(--ext);
    2371 
    2372             ////UnshareConcreteFlyweight uf = new UnshareConcreteFlyweight();
    2373             ////uf.Operation(--ext);
    2374 
    2375             //WebSiteFactory f = new WebSiteFactory();
    2376 
    2377             //WebSite fx = f.GetWebSiteCategory("产品展示");
    2378             //fx.Use();
    2379 
    2380             //WebSite fy = f.GetWebSiteCategory("产品展示");
    2381             //fy.Use();
    2382 
    2383             //WebSite f1 = f.GetWebSiteCategory("博客");
    2384             //f1.Use();
    2385 
    2386             //WebSite f2 = f.GetWebSiteCategory("博客");
    2387             //f2.Use();
    2388 
    2389             //Console.WriteLine("网站分类总数为{0}",f.GetWebSiteCount());
    2390             #endregion
    2391 
    2392             #region 解释器模式
    2393             ////给定一个语言,定义它的文法的一种表示,
    2394             ////并定义一个解释器,
    2395             ////这个解释器使用该表示来解释语言中的句子。
    2396             //Context1 context = new Context1();
    2397             //IList<AbstractExpression> list = new List<AbstractExpression>();
    2398             //list.Add(new TerminalExpression());
    2399             //list.Add(new NoterminalExpression());
    2400             //list.Add(new TerminalExpression());
    2401             //list.Add(new TerminalExpression());
    2402 
    2403             //foreach (AbstractExpression exp in list)
    2404             //{
    2405             //    exp.Interpret(context);
    2406             //}
    2407             #endregion
    2408 
    2409             #region 访问者模式
    2410             ////表示一个作用于某对象结构的各元素操作。
    2411             ////它使你可以在不改变各元素的类的前提下
    2412             ////定义作用于这些元素的新操作。
    2413             //ObjectStructure o = new ObjectStructure();
    2414             //o.Attach(new ConcreteElementA());
    2415             //o.Attach(new ConcreteElementB());
    2416 
    2417             //ConcreteVisitorA v1 = new ConcreteVisitorA();
    2418             //ConcreteVisitorB v2 = new ConcreteVisitorB();
    2419             //o.Accept(v1);
    2420             //o.Accept(v2);
    2421             #endregion
    2422         }
    2423     }
    2424 }
    大话设计模式代码

    源码地址: https://github.com/doyoulaikeme/DesignPatterns

    好好学习,天天向上。
  • 相关阅读:
    整合规则引擎urule
    vue学习
    发送put请求,get请求
    jpa自定义字段
    spring的3种配置方式
    netty
    springsercurity和shiro
    git报错
    Scrapy全站数据爬取
    python操作Excel模块openpyxl
  • 原文地址:https://www.cnblogs.com/Zhengxue/p/8807807.html
Copyright © 2020-2023  润新知