Beispiel #1
0
        //访问者
        public static void testVisitor()
        {
            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);

            Amativeness v3 = new Amativeness();

            o.Display(v3);

            Marriage v4 = new Marriage();

            o.Display(v4);

            Console.Read();
        }
Beispiel #2
0
    void TestVisitor()
    {
        ObjectStructure structure = new ObjectStructure();

        structure.Attach(new Man());
        structure.Attach(new Woman());

        Success success = new Success();

        structure.Display(success);

        Failing failing = new Failing();

        structure.Display(failing);

        Amativeness amativeness = new Amativeness();

        structure.Display(amativeness);
    }
Beispiel #3
0
        public void VistorTest()
        {
            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);

            Amativeness v3 = new Amativeness();

            o.Display(v3);

            Marriage v4 = new Marriage();

            o.Display(v4);
        }
Beispiel #4
0
 /// <summary>
 /// Non-empty opt projection
 /// </summary>
 public static val Some <val>(this Opt <val> that) =>
 that.Case(Failing <val> .Tag(that, nameof(Some)), (t, _) => t.ToValue(), Unit(), Snd);
Beispiel #5
0
 /// <summary>
 /// Empty opt projection
 /// </summary>
 public static Unit None <val>(this Opt <val> that) =>
 that.Case(Unit(), Snd, Failing <Unit> .Tag(that, nameof(None)), (t, _) => t.ToValue());
Beispiel #6
0
 /// <summary>
 /// `Greater than` projection
 /// </summary>
 public static Unit Gt(this Ord that) =>
 that.Case(Failing <Unit> .Tag(that, nameof(Lt)), Failing <Unit> .Tag(that, nameof(Lt)), Id);
Beispiel #7
0
 /// <summary>
 /// `Less than` projection
 /// </summary>
 public static Unit Lt(this Ord that) =>
 that.Case(Id, Failing <Unit> .Tag(that, nameof(Lt)), Failing <Unit> .Tag(that, nameof(Lt)));
Beispiel #8
0
 /// <summary>
 /// Right projection
 /// </summary>
 public static right Right <left, right>(this Either <left, right> that) =>
 that.Case(Failing <right> .Tag(that, nameof(Right)), (t, _) => t.ToValue(), Unit(), Snd);
Beispiel #9
0
 /// <summary>
 /// Left projection
 /// </summary>
 public static left Left <left, right>(this Either <left, right> that) =>
 that.Case(Unit(), Snd, Failing <left> .Tag(that, nameof(Left)), (t, _) => t.ToValue());
Beispiel #10
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("==========时间格式化-本地文化============");
        }
Beispiel #11
0
 /// <summary>
 /// Non-empty list projection
 /// </summary>
 public static Pair <val, List <val> > Cons <val>(this List <val> that) =>
 that.Case(Failing <Pair <val, List <val> > > .Tag(that, nameof(Cons)), (t, _) => t.ToValue(), Unit(), Snd);
Beispiel #12
0
 /// <summary>
 /// Empty list projection
 /// </summary>
 public static Unit Nil <val>(this List <val> that) =>
 that.Case(Unit(), Snd, Failing <Unit> .Tag(that, nameof(Nil)), (t, _) => t.ToValue());