Exemplo n.º 1
0
    static void Main(string[] args)
    {
        Person xc = new Person("小菜");

        Console.WriteLine("\n第一种装扮:");

        // 实例化具体服饰
        Finery dtx = new TShirts();
        Finery kk  = new BigTrouser();
        Finery pqx = new Sneakers();

        dtx.Show();
        kk.Show();
        pqx.Show();
        xc.Show();

        Console.WriteLine("\n第二种装扮:");

        Finery xz = new Suit();
        Finery ld = new Tie();
        Finery px = new LeatherShoes();

        xz.Show();
        ld.Show();
        px.Show();
        xc.Show();

        Console.Read();
    }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Person xc = new Person("小菜");

            Console.WriteLine("第一種裝扮 : ");

            TShirts    ts = new TShirts();
            BigTrouser bt = new BigTrouser();

            ts.Decorate(xc);
            bt.Decorate(ts);

            bt.show();

            /*
             * 使用完這個模式可以聯想到,之前前輩使用command pattern來應付不同的使用者需求,
             * 因此進來什麼command,就執行該command所屬類別的動作,這樣的前提是這些command類別都要定義出相同的function名稱,
             * 而decorate pattern似乎也要定義出相同的function名稱,只是他可以串接不同的程式碼,
             * 那麼這兩個模式的差異在哪呢?
             * 仔細思考一下,前輩使用的command pattern是為了包容不同類別的物件去執行不同的function內容,
             * 算是平行式的區分不同的類別,並統一在一個程式碼位置去執行,
             * 而decorate pattern是直列式的串接不同的function內容,比較像是有組合function內容的彈性,
             * 應該可以套用到前輩的command pattern之上,讓不同類別的物件有一個執行順序。
             *
             * 兩者的用途目前還有點混亂,
             * 總結來說:
             * command pattern 可以簡化啟動不同類別物件的啟動位置,濃縮成一個,也就是說不管傳進來的物件是誰,他都可以啟動。
             * decorate pattern 可以串接不同功能到同一個物件上,再由最後一個物件去啟動。
             *
             * 應用端來說:
             * 先透過decorate pattern把需要的功能串接起來,統一丟到command pattern的啟動位置去啟動。
             *
             * 又或許說,友崧用的只是多型的概念,還不是真正的command pattern。
             */
        }
Exemplo n.º 3
0
        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();
        }
Exemplo n.º 4
0
        public void Decoratot()
        {
            // 可參考PaymentController SetPayDataStatusToPayment
            var person = new Person("小菜");

            Console.WriteLine($"第一種裝扮");
            var pqx = new Sneakers();
            var kk  = new Trousers();
            var dtx = new TShirts();

            pqx.Decorate(person);
            kk.Decorate(pqx);
            dtx.Decorate(kk);
            dtx.Show();

            Console.WriteLine($"第二種裝扮");
            var px = new LeatherShoes();
            var ld = new Tie();
            var xz = new BusinessSuit();

            px.Decorate(person);
            ld.Decorate(px);
            xz.Decorate(ld);
            xz.Show();
        }
Exemplo n.º 5
0
        public void Decoratot()
        {
            var person = new Person("小菜");

            // 第一種裝扮
            var pqx = new Sneakers();
            var kk  = new Trousers();
            var dtx = new TShirts();

            pqx.Decorate(person);
            kk.Decorate(pqx);
            dtx.Decorate(kk);
            dtx.Show();
        }
Exemplo n.º 6
0
        /// <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();
        }
Exemplo n.º 7
0
    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();
    }
Exemplo n.º 8
0
        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();
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
    void TestDecorator()
    {
        Person component = new Person("Alfred");

        Debug.Log("First Clothes");

        Trouser trouser = new Trouser();
        TShirts tshirts = new TShirts();

        trouser.Decorate(component);
        tshirts.Decorate(trouser);
        tshirts.Show();

        Debug.Log("Second Clothes:");
        Sneaker sneaker = new Sneaker();
        Shoes   shoes   = new Shoes();

        sneaker.Decorate(component);
        shoes.Decorate(sneaker);
        shoes.Show();
    }
Exemplo n.º 11
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("==========时间格式化-本地文化============");
        }