Esempio n. 1
0
        static void ShowStrategyPattern(State eParam, string sParam)
        {
            StrategyContext stateContext = new StrategyContext();
            StrategyBase    strategyBase = null;

            switch (eParam)
            {
            case State.State1:
                strategyBase = new StrategyOne();
                break;

            case State.State2:
                strategyBase = new StrategyTwo();
                break;

            case State.State3:
                strategyBase = new StrategyThree();
                break;

            default:
                throw new Exception("int参数只能是1,2,3");
            }
            strategyBase.Invoke(eParam, sParam);//可以这么做,但这样就不能额外做一些其他的操作了
            stateContext.Invoke(eParam, sParam, strategyBase);
        }
        public async Task CanCallRouterWithDifferentRouterStrategies()
        {
            using (var fixture = new RoutingFixture())
            {
                var serviceName1 = fixture.GetNewServiceName();
                var route1       = "/data";

                var strategyOne = new StrategyOne();
                var strategyTwo = new StrategyTwo();

                fixture.AddService(serviceName1, route1);

                fixture.AddRouter(strategyOne, strategyTwo);

                fixture.StartAll();

                await fixture.WaitForRegistrationAsync();

                var routerResponse = await fixture.CallRouterAsync("/admin/condenser/router/replace?strategy=One");

                Assert.Equal(HttpStatusCode.OK, routerResponse.StatusCode);

                var responseService = await fixture.CallRouterAsync(route1);

                Assert.Equal(HttpStatusCode.OK, responseService.StatusCode);
                Assert.True(strategyOne.Called, "strategy one was not called");

                routerResponse = await fixture.CallRouterAsync("/admin/condenser/router/replace?strategy=Two");

                Assert.Equal(HttpStatusCode.OK, routerResponse.StatusCode);

                responseService = await fixture.CallRouterAsync(route1);

                Assert.Equal(HttpStatusCode.OK, responseService.StatusCode);
                Assert.True(strategyTwo.Called, "strategy two was not called");
            }
        }
        private static void Main()
        {
            // Facade.
            {
                Console.WriteLine("----    Testing Facade pattern    ----");
                var swimmingPoodSystemsFacade = new SwimmingpoolFacade();
                swimmingPoodSystemsFacade.GoToASwimmingPool();
                Console.WriteLine("----    Testing Facade pattern finished    ----");
                Console.WriteLine(".. press any key to continue testing ..");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Mediator
            {
                Console.WriteLine("----    Testing Mediator pattern    ----");

                var brain = new BrainMediator();
                var ear   = new Ear(brain);
                var face  = new Face(brain);

                brain.Ear  = ear;
                brain.Face = face;

                ear.HearSounds();

                Console.WriteLine("----    Testing Mediator pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Singleton
            {
                Console.WriteLine("----    Testing Singleton pattern    ----");

                var loggerSingleton = LoggerSingleton.GetInstance();
                loggerSingleton.TestId = 7;

                var loggerSingleton2 = LoggerSingleton.GetInstance();

                Console.WriteLine(loggerSingleton2.ToString());

                Console.WriteLine("----    Testing Singleton pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Builder
            {
                Console.WriteLine("----    Testing Builder pattern    ----");

                var gamingBuilder    = new GamingLaptopBuilder();
                var travelingBuilder = new TravelingLaptopBuilder();
                var director         = new BuilderDirector(travelingBuilder);

                var laptop = director.ConstructLaptop();

                Console.WriteLine(laptop.ToString());

                director.ChangeBuilderTo(gamingBuilder);

                var laptop2 = director.ConstructLaptop();

                Console.WriteLine(laptop2.ToString());

                Console.WriteLine("----    Testing Builder pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Factory Method
            {
                Console.WriteLine("----    Testing Factory Method pattern    ----");

                Console.WriteLine(ConcreteFactory.CreateProduct(ProductType.Type2));

                Console.WriteLine("----    Testing Factory Method pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Abstract Factory
            {
                Console.WriteLine("----    Testing Abstract Factory pattern    ----");

                AbstractToysFactory factory = new TeddyToyFactory();
                var createdToy = factory.CreateToy();
                Console.WriteLine(createdToy.ToString());

                Console.WriteLine("----    Testing Abstract Factory pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Prototype
            {
                Console.WriteLine("----    Testing Prototype pattern    ----");

                var prototype = ProtorypeDirector.GetFilledPrototype();
                prototype.Parameter2 = prototype.Parameter2 + " changed!";
                Console.WriteLine(prototype.ToString());

                Console.WriteLine("----    Testing Prototype pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Strategy
            {
                Console.WriteLine("----    Testing Strategy pattern    ----");

                var strategyOne = new StrategyOne();
                var strategyTwo = new StrategyTwo();

                StrategyClient.DoJobUsingSomeStrategy(strategyOne, "data parameter");
                StrategyClient.DoJobUsingSomeStrategy(strategyTwo, "data parameter");

                Console.WriteLine("----    Testing Strategy pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Template Method
            {
                Console.WriteLine("----    Testing Template Method pattern    ----");

                var templateMethodImplementationClassObject = new ConcreteClassWithTemplateMethodUsing();
                templateMethodImplementationClassObject.DoSomeTemplateMethodJob();

                Console.WriteLine("----    Testing Template Method pattern finished    ----");
                Console.ReadKey();
            }

            Console.WriteLine("--------------------------------------------------");

            // Iterator
            {
                Console.WriteLine("----    Testing Iterator pattern    ----");

                var iterator = new EnumerableDataSource(new[] { "one", "two" });

                foreach (var item in iterator)
                {
                    Console.WriteLine(item);
                }

                Console.WriteLine("----    Testing Iterator pattern finished    ----");
                Console.ReadKey();
            }
        }