Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Resume a = new Resume("小明");
            a.SetPersonalInfo("男","23");
            a.SetWorkExperience("2010-2015","XXX公司");
            //a.DisPlay();

            Resume b = (Resume)a.Clone();
            b.SetWorkExperience("1998-2008","YYY企业");

            Resume c = (Resume)a.Clone();
            c.SetPersonalInfo("男", "25");

            a.DisPlay();
            b.DisPlay();
            c.DisPlay();

            Console.ReadKey();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Resume a = new Resume("大鳥");

            a.SetPersonInfo("男", "29");
            a.SetWorkExperience("1998-2000", "xxx公司");

            Resume b = (Resume)a.Clone();

            b.SetWorkExperience("1998-2006", "yyy公司");

            Resume c = (Resume)a.Clone();

            c.SetPersonInfo("男", "24");

            a.Display();
            b.Display();
            c.Display();
            Console.Read();
        }
Exemplo n.º 3
0
        public void Prototype()
        {
            // 淺複製
            Resume resume = new Resume("大鳥");

            resume.SetPersonalInfo("男", "29");
            resume.SetWorkWxpweience("1998-2000", "XX公司");

            Resume resume1 = (Resume)resume.Clone();

            resume1.SetWorkWxpweience("1998-2006", "YY企業");

            Resume resume2 = (Resume)resume.Clone();

            resume2.SetPersonalInfo("男", "24");

            resume.Display();
            resume1.Display();
            resume2.Display();

            // 深複製
            ResumeV2 resume3 = new ResumeV2("大鳥");

            resume3.SetPersonalInfo("男", "29");
            resume3.SetWorkWxpweience("1998-2000", "XX公司");

            ResumeV2 resume4 = (ResumeV2)resume3.Clone();

            resume4.SetWorkWxpweience("1998-2006", "YY企業");

            ResumeV2 resume5 = (ResumeV2)resume3.Clone();

            resume5.SetPersonalInfo("男", "24");

            resume3.Display();
            resume4.Display();
            resume5.Display();
        }
Exemplo n.º 4
0
        public void ExecuteDeepClone()
        {
            //Deep Clone
            WorkExperience s = new WorkExperience();

            s.Company  = "IT";
            s.WorkDate = "2018-01-01";

            Resume x1 = new Resume(s);

            x1.SetPersonalInfo("女", "18");
            x1.SetWorkExperience("2018-0202", "HTC");
            var x2 = x1.Clone();
        }
        public void Run()
        {
            Resume resume = new Resume();

            resume.Name = "求职者";
            resume.Sex  = "男";
            resume.Age  = 25;
            resume.Work = new WorkExperience {
                Company = "深圳XX公司", WorkDate = "2010-2016"
            };

            Resume rs2 = (Resume)resume.Clone();
            Resume rs3 = (Resume)rs2.Clone();

            rs2.Work.Company    = "深圳YY公司";
            resume.Work.Company = "深圳ZZ公司";

            Console.WriteLine($"{resume.Name},{resume.Sex},{resume.Age},曾在{resume.Work.WorkDate}就职于{resume.Work.Company}");
            Console.WriteLine($"{rs2.Name},{rs2.Sex},{rs2.Age},曾在{rs2.Work.WorkDate}就职于{rs2.Work.Company}");
            Console.WriteLine($"{rs3.Name},{rs3.Sex},{rs3.Age},曾在{rs3.Work.WorkDate}就职于{rs3.Work.Company}");
        }
Exemplo n.º 6
0
        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("==========时间格式化-本地文化============");
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            #region Strategy
            Console.WriteLine("------------Strategy------------------");
            Console.WriteLine("\r\n ------Strategy Test");
            {
                CashNormal cn = new CashNormal();
                Context    ct = new Context(cn);
                ct.PayAmount(100);

                CashDiscount cd = new CashDiscount(0.1);
                ct = new Context(cd);
                ct.PayAmount(100);

                CashReturn cr = new CashReturn(300, 100);
                ct = new Context(cr);
                ct.PayAmount(400);
            }
            #endregion

            #region Observer
            Console.WriteLine("------------Observer------------------");
            Console.WriteLine("\r\n ------Observer Test");
            SydneyStock   ss  = new SydneyStock();
            StockObserver ob1 = new StockObserver();
            ss.Register(ob1);
            ss.Notify(100);
            ss.Register(new StockObserver());
            ss.Notify(200);
            ss.Unregister(ob1);
            ss.Notify(300);
            #endregion

            #region Factory
            Console.WriteLine("------------Factory------------------");
            Console.WriteLine("\r\n ------Factory Test");
            WarshipFactory wf = new WarshipFactory();
            String         s  = "A";
            Warship        ws = wf.GetWarship(s);
            if (ws != null)
            {
                ws.Fight();
            }

            s  = "B";
            ws = wf.GetWarship(s);
            if (ws != null)
            {
                ws.Fight();
            }
            #endregion

            #region Abstract Factory
            Console.WriteLine("------------Abstract Factory------------------");
            Console.WriteLine("\r\n ------Abstract Factory Test");
            IRestaurant ir = new ChineseRestaurant();
            IVegetable  iv = ir.CookVegetable();
            iv.Display();
            IMeat im = ir.CookMeat();
            im.Display();

            ir = new AustralianRestaurant();
            iv = ir.CookVegetable();
            iv.Display();
            im = ir.CookMeat();
            im.Display();
            #endregion

            #region Singleton
            Console.WriteLine("------------Singleton------------------");
            Console.WriteLine("\r\n ------Singleton Test");
            MySingleton ms1 = MySingleton.GetInstance();
            MySingleton ms2 = MySingleton.GetInstance();
            if (ms1 == ms2)
            {
                Console.WriteLine("Same Instance of Singleton");
            }
            #endregion

            #region Builder
            Console.WriteLine("------------Builder------------------");
            Console.WriteLine("\r\n ------Builder Test");
            {
                IPersonBuilder        pb  = new FatPersonBuilder();
                PersonBuilderDirector pbd = new PersonBuilderDirector(pb);
                pbd.BuildPerson();
                pbd = new PersonBuilderDirector(new ThinPersonBuilder());
                pbd.BuildPerson();
            }
            #endregion

            #region AdvanceBuilder
            Console.WriteLine("\r\n ------AdvanceBuilder Test");
            {
                PersonBuilderDirectorBase pbd = new PersonBuilderDirector1();
                pbd.SetPersonBuilder(new FatPersonBuilder());
                pbd.BuildPerson();
                pbd.SetPersonBuilder(new ThinPersonBuilder());
                pbd.BuildPerson();
                Console.WriteLine();
                pbd = new PersonBuilderDirector2();
                pbd.SetPersonBuilder(new FatPersonBuilder());
                pbd.BuildPerson();
                pbd.SetPersonBuilder(new ThinPersonBuilder());
                pbd.BuildPerson();
            }
            #endregion

            #region Prototype
            Console.WriteLine("------------Prototype------------------");
            Resume r1 = new Resume();
            r1.id   = 1;
            r1.Name = "P1";
            r1.Experience.Company = "C1";
            Console.WriteLine(r1);
            Resume r2 = (Resume)r1.Clone();
            r2.id   = 2;
            r2.Name = "P2";
            r2.Experience.Company = "C2";
            Console.WriteLine(r2);
            #endregion

            #region Decorator
            Console.WriteLine("------------Decorator------------------");
            Pizza        pp = new PlainPizza();
            ToppingPizza tp = new Mozzarella(pp);
            ToppingPizza ts = new TomatoSauce(tp);
            ts.GetDescription();

            #endregion

            #region Command
            Console.WriteLine("------------Command------------------");
            Cook            barbecuerCook    = new BarbecuerCook();
            Cook            fryDishCook      = new FryDishCook();
            Command.Command barbecuerCommand = new BarbecuerCommand(barbecuerCook);
            Command.Command fryDishCommand   = new FryDishCommand(fryDishCook);

            Waiter waiter = new Waiter();
            waiter.AddCommand(barbecuerCommand);
            waiter.AddCommand(fryDishCommand);
            waiter.Notify();

            #endregion

            #region Adapter
            Console.WriteLine("------------Adapter------------------");
            BasketballPlayer        bp1 = new Forward();
            BasketballPlayer        bp2 = new Guard();
            ChineseCenter           cc  = new ChineseCenter();
            BasketballPlayer        bp3 = new CenterInterPreter(cc);
            List <BasketballPlayer> bps = new List <BasketballPlayer>();
            bps.Add(bp1);
            bps.Add(bp2);
            bps.Add(bp3);
            foreach (var item in bps)
            {
                item.Attack();
                item.Defense();
            }
            #endregion

            #region Facade
            Console.WriteLine("------------Facade------------------");
            FundFacade ff = new FundFacade();
            ff.Buy();
            ff.Sell();
            #endregion


            #region Bridge
            {
                Console.WriteLine("------------Bridge---------------------");
                IProtocol     udp = new Bridge.Udp();
                IProtocol     tcp = new Bridge.Tcp();
                Communication c   = new Communication();
                c.SetProtocol(udp);
                c.Work();
                c.SetProtocol(tcp);
                c.Work();
            }
            #endregion

            #region Template
            {
                Console.WriteLine("------------Template-----------------------");
                MediaProtocol tcp = new Template.Tcp();
                tcp.Work();
                MediaProtocol udp = new Template.Udp();
                udp.Work();
            }

            #endregion

            #region Composite
            {
                Console.WriteLine("------------Composite-----------------------");
                FileComponent First = new FileFolder()
                {
                    Name = "C"
                };
                FileComponent second1 = new FileFolder()
                {
                    Name = "-Folder1"
                };
                FileComponent second2 = new File()
                {
                    Name = "-File2"
                };
                FileComponent second1_1 = new File()
                {
                    Name = "--File1_1"
                };
                second1.Add(second1_1);
                First.Add(second1);
                First.Add(second2);
                First.Display();
            }
            #endregion

            Console.ReadLine();
        }