static void Test1() { var person = new Person("张三"); // 装饰模式 /* * Component: 定义好的对象接口 可以给这个对象动态地添加职责 -- Person类 * ConcreateComponent: 定义了一个具体的对象 也可以给这个对象添加一些职责 --这里没用到 * Decorator: 装饰抽象类 继承了Component 从外类来扩展Component类的功能 -- Finery类 * ConcreateDecoratorA: 具体的装饰对象 继承了Decorator类 起到给Component添加职责的功能 --TShirts * ConcreateDecoratorB */ // 实例化具体的服饰对象 var tsh = new TShirts(); var bigTh = new BigTrouser(); // 开始装饰 其实可以通过构造函数进行打扮(添加职责) tsh.Decorate(person); bigTh.Decorate(tsh); // show bigTh.Show(); }
/// <summary> /// 装饰模式 /// </summary> static void TestDecorator() { Person xiaocai = new Person("xiaocai"); Console.WriteLine("\n第一种装扮:"); Sneakers sneaker = new Sneakers(); BigTrouser bigTrouser = new BigTrouser(); TShirts shirts = new TShirts(); sneaker.Decorate(xiaocai); bigTrouser.Decorate(sneaker); shirts.Decorate(bigTrouser); shirts.Show(); }
public void Run() { // 人物 Person person = new Person("李小妹"); // 装饰内容 Tshirts tshirts = new Tshirts(); BigTrouser bigTrouser = new BigTrouser(); // 装饰 tshirts.Decorate(person); bigTrouser.Decorate(tshirts); // 显示 bigTrouser.Show(); }
static void Main(string[] args) { Person xc = new Person("小菜"); Console.WriteLine("\n第一种装扮:"); Sneakers pqx = new Sneakers(); BigTrouser kk = new BigTrouser(); TShirts dtx = new TShirts(); // 一层层嵌套、后进先出 pqx.Decorate(xc); kk.Decorate(pqx); dtx.Decorate(kk); dtx.Show(); // 大T恤 垮裤 破球鞋 装扮的小菜 Console.WriteLine("\n第二种装扮:"); LeatherShoes px = new LeatherShoes(); Tie ld = new Tie(); Suit xz = new Suit(); // 一层层嵌套、后进先出 px.Decorate(xc); ld.Decorate(px); xz.Decorate(ld); xz.Show(); Console.WriteLine("\n第三种装扮:"); Sneakers pqx2 = new Sneakers(); LeatherShoes px2 = new LeatherShoes(); BigTrouser kk2 = new BigTrouser(); Tie ld2 = new Tie(); // 一层层嵌套、后进先出 pqx2.Decorate(xc); px2.Decorate(pqx); kk2.Decorate(px2); ld2.Decorate(kk2); ld2.Show(); // 领带 垮裤 皮鞋 破球鞋 装扮的小菜 Console.Read(); }
public void Run() { Person person = new Person("骚货在"); TShirts tshirts = new TShirts(); BigTrouser trouser = new BigTrouser(); LeatherShoes shoes = new LeatherShoes(); Suit suit = new Suit(); Sneakers sneaker = new Sneakers(); Tie tie = new Tie(); tshirts.Decorate(person); trouser.Decorate(tshirts); shoes.Decorate(trouser); suit.Decorate(shoes); sneaker.Decorate(suit); tie.Decorate(sneaker); tie.Show(); }
static void Main(string[] args) { Person xc = new Person("小菜"); Console.WriteLine(Environment.NewLine + "第一种装扮:"); Sneakers pqx = new Sneakers(); BigTrouser kk = new BigTrouser(); TShirts dtx = new TShirts(); pqx.Decorate(xc); kk.Decorate(pqx); dtx.Decorate(kk); dtx.Show(); Console.WriteLine(Environment.NewLine + "第二种装扮:"); LeatherShoes px = new LeatherShoes(); Tie ld = new Tie(); Suit xz = new Suit(); px.Decorate(xc); ld.Decorate(px); xz.Decorate(ld); xz.Show(); Console.WriteLine(Environment.NewLine + "第三种装扮:"); Sneakers pqx2 = new Sneakers(); LeatherShoes px2 = new LeatherShoes(); BigTrouser kk2 = new BigTrouser(); Tie ld2 = new Tie(); pqx2.Decorate(xc); px2.Decorate(pqx2); kk2.Decorate(px2); ld2.Decorate(kk2); ld2.Show(); Console.ReadKey(true); }
static void Main(string[] args) { #if DECORATOR Decorator.Person ms = new Decorator.Person("MarsonShine"); Console.WriteLine("\n 第一种妆扮:"); TShirts dtx = new TShirts(); BigTrouser bt = new BigTrouser(); dtx.Decorate(ms); bt.Decorate(dtx); bt.Show(); #endif #if Proxy SchoolGirl zhuqin = new SchoolGirl(); zhuqin.Name = "祝琴"; Proxy.Proxy ms = new Proxy.Proxy(zhuqin); ms.GiveChocolate(); ms.GiveDolls(); ms.GiveFlowers(); Console.ReadLine(); #endif #if ChanOfResposibility HandsetBrand hb; hb = new HandsetBrandN(); hb.SetHandsetSoft(new HandsetGame()); hb.Run(); hb.SetHandsetSoft(new HandsetAddressList()); hb.Run(); HandsetBrand hb2; hb2 = new HandsetBrandM(); hb2.SetHandsetSoft(new HandsetGame()); hb2.Run(); hb2.SetHandsetSoft(new HandsetAddressList()); hb2.Run(); #endif #if ChainOfResiposibility CommonManager jinli = new CommonManager("jinli"); Majordomo zongjian = new Majordomo("zongjian"); GeneralManager zhongjingli = new GeneralManager("zhongjinli"); jinli.SetSuperior(jinli); zongjian.SetSuperior(zhongjingli); Request request = new Request(); request.RequestType = "请假"; request.RequestContent = "我要请假"; request.Number = 1; jinli.RequestApplications(request); Request request2 = new Request(); request2.RequestType = "请假"; request2.RequestContent = "我要请假"; request.Number = 4; jinli.RequestApplications(request2); Request request3 = new Request(); request3.RequestType = "请假"; request3.RequestContent = "我还是要请假"; request.Number = 500; jinli.RequestApplications(request3); #endif ObjectStructure o = new ObjectStructure(); o.Attach(new Man()); o.Attach(new Woman()); Success v1 = new Success(); o.Display(v1); Failing v2 = new Failing(); o.Display(v2); // 根据业务需求得知文件格式 var fileType = Enum.Parse <FileType>("Word"); var wordConvertor = PdfConvertorFactory.Create(fileType); wordConvertor.Convert("example.docx"); fileType = Enum.Parse <FileType>("Wps"); var wpsConvertor = PdfConvertorFactory.Create(fileType); wpsConvertor.Convert("example.wps"); // 策略模式 var vertor = new Strategy.WordToPdfConvertor(); var strategy = new StrategyContext(vertor); strategy.DoWork("example.docx"); var excel = new Strategy.ExcelToPdfConvertor(); strategy = new StrategyContext(excel); strategy.DoWork("example.xlsx"); // 策略模式+工厂模式 封装部分相同逻辑,又有部分业务不同的逻辑变化 // 抽象工厂模式 IConvertorFactory factory = new WordToPdfConvertorFactory(); Console.WriteLine("==========抽象工厂============"); factory.Create().Convert("example.docx"); // 原型模式 Console.WriteLine("==========原型模式============"); Resume r = new Resume("marson shine"); r.SetPersonalInfo("男", "27"); r.SetWorkExperience("6", "kingdee.cpl"); r.Display(); // 如果我要复制三个 Resume,则不需要实例化三次,而是调用Clone()即可 var r2 = (Resume)r.Clone(); var r3 = (Resume)r.Clone(); r2.SetWorkExperience("5", "yhglobal.cpl"); r2.Display(); r3.SetWorkExperience("3", "che100.cpl"); r3.Display(); // 观察者模式 Console.WriteLine("==========观察者模式============"); StudentOnDuty studentOnDuty = new StudentOnDuty(); var student = new StudentObserver("marson shine", studentOnDuty); studentOnDuty.Attach(student); studentOnDuty.Attach(new StudentObserver("summer zhu", studentOnDuty)); studentOnDuty.Notify(); studentOnDuty.UpdateEvent += student.Update; Console.WriteLine("==========观察者模式============"); Console.WriteLine("==========状态模式============"); var client = new Client(new PerfectState()); client.Handle(); client.Handle(); client.Handle(); Console.WriteLine("==========状态模式============"); Console.WriteLine("==========备忘录模式============"); var originator = new Originator(); originator.State = "On"; originator.Show(); MementoManager manager = new MementoManager(); manager.Record(originator.CreateMemento()); originator.State = "Off"; originator.Show(); originator.SetMemento(manager.Memento); originator.Show(); Console.WriteLine("==========备忘录模式============"); Console.WriteLine("==========规格模式============"); // 只要华为品牌的手机 ISpecification <Mobile> huaweiExpression = new ExpressionSpecification <Mobile>(p => p.Type == "华为"); // 三星手机 ISpecification <Mobile> samsungExpression = new ExpressionSpecification <Mobile>(p => p.Type == "三星"); // 华为和三星 ISpecification <Mobile> huaweiAndsamsungExpression = huaweiExpression.And(samsungExpression); List <Mobile> mobiles = new List <Mobile> { new Mobile("华为", 4888), new Mobile("三星", 6888), new Mobile("苹果", 7888), new Mobile("小米", 3888) }; var samsungs = mobiles.FindAll(p => samsungExpression.IsSatisfiedBy(p)); var huaweis = mobiles.FindAll(p => huaweiExpression.IsSatisfiedBy(p)); var samsungAndhuaweis = mobiles.FindAll(p => huaweiAndsamsungExpression.IsSatisfiedBy(p)); Console.WriteLine("==========规格模式============"); Console.WriteLine("==========时间格式化-本地文化============"); Console.WriteLine(DateTime.Now.ToString()); Console.WriteLine("ShortDatePattern:" + CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern); Console.WriteLine("LongDatePattern:" + CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern); Console.WriteLine("LongTimePattern:" + CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern); CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone(); culture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd"; culture.DateTimeFormat.LongTimePattern = "h:mm:ss.fff"; CultureInfo.CurrentCulture = culture; Console.WriteLine(DateTime.Now.ToString()); Console.WriteLine("开始后台线程时间格式化"); Task.Run(() => { Console.WriteLine("后台线程:" + DateTime.Now.ToString()); }); Console.WriteLine("==========时间格式化-本地文化============"); }