Beispiel #1
0
        static void Main()
        {
            Duck littleDuck = new RubberDuck();
            Duck woodenDuck = new WoodenDuck();
            Duck simleDuck  = new SimpleDuck();
            Duck fakeDuck   = new FakeDuck();

            Console.WriteLine("1st duck");
            littleDuck.PerformFly();
            littleDuck.PerformQuack();
            littleDuck.Swim();

            Console.WriteLine("\n");

            Console.WriteLine("2nd duck");
            woodenDuck.PerformFly();
            woodenDuck.PerformQuack();
            woodenDuck.Swim();

            Console.WriteLine("\n");

            Console.WriteLine("3rd duck");
            simleDuck.PerformFly();
            simleDuck.PerformQuack();
            simleDuck.Swim();

            Console.WriteLine("\n");

            Console.WriteLine("4th duck");
            fakeDuck.PerformFly();
            fakeDuck.PerformQuack();
            fakeDuck.Swim();

            Console.ReadKey();
        }
Beispiel #2
0
        private static void Main()
        {
            var mallardDuck = new MallardDuck();

            mallardDuck.Display();
            mallardDuck.PerformQuack();
            mallardDuck.Swim();
            mallardDuck.PerformFly();
            Console.WriteLine();

            var redheadDuck = new RedheadDuck();

            redheadDuck.Display();
            redheadDuck.PerformQuack();
            redheadDuck.Swim();
            redheadDuck.PerformFly();
            Console.WriteLine();

            var rubberDuck = new RubberDuck();

            rubberDuck.Display();
            rubberDuck.PerformQuack();
            rubberDuck.Swim();
            rubberDuck.PerformFly();
            Console.WriteLine();

            var decoyDuck = new DecoyDuck();

            decoyDuck.Display();
            decoyDuck.Swim();
            Console.WriteLine();

            Console.Write("Press Enter to exit ...");
            Console.ReadLine();
        }
Beispiel #3
0
        public void FakeDucksCantFly()
        {
            RubberDuck rd = new RubberDuck();
            DecoyDuck  dd = new DecoyDuck();

            rd.PerformFly();
            dd.PerformFly();
        }
Beispiel #4
0
        internal static void ExecuteStrategy()
        {
            //Intent
            //  • Define a family of algorithms, encapsulate each one, and make them interchangeable.Strategy lets the algorithm vary independently from
            //      the clients that use it.
            //  • Capture the abstraction in an interface, bury implementation details in derived classes.

            Strategy.Duck greenDuck = new GreenDuck();
            greenDuck.PerformFly();
            greenDuck.PerformQuack();

            Strategy.Duck rubberDuck = new RubberDuck();
            rubberDuck.PerformFly();
            rubberDuck.PerformQuack();

            rubberDuck.SetFlyBehaviour(new FlyWithRockets()); // Changing behaviour at runtime !
            rubberDuck.PerformFly();
        }
        public void TestRubberDuck()

        {
            HeadFirstDesignPatterns.Strategy.Duck.Duck RubberDuck = new RubberDuck();

            Assert.AreEqual("Squeak", RubberDuck.PerformQuack());

            Assert.AreEqual("I can't fly.", RubberDuck.PerformFly());
        }
Beispiel #6
0
        public void RubberDuck_ShouldFlyRocketPowered_WhenChangeFlyRocketPowered()
        {
            //Arrange
            Duck rubber = new RubberDuck();

            //Act
            rubber.FlyBehavior = new FlyRocketPowered();
            var fly = rubber.PerformFly();

            //Assert
            fly.Should().Be("I'm flying with a rocket!");
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            RedheadDuck duckduck = new RedheadDuck();

            duckduck.PerformFly();
            duckduck.PerformQuack();
            Duck duck = new RubberDuck();

            duck.PerformFly();
            duck.PerformQuack();
            duck.QuackBehaviour = new QuackOne();
            duck.PerformQuack();
        }
Beispiel #8
0
        public void RubberDuck_ShouldCantFlyAndSqueakAndDisplay()
        {
            //Arrange
            Duck rubber = new RubberDuck();

            //Act
            var fly     = rubber.PerformFly();
            var quack   = rubber.PerformQuack();
            var display = rubber.Display();

            //Assert
            fly.Should().Be("I can't fly.");
            quack.Should().Be("Squeak");
            display.Should().Be("I'm a Rubber duck");
        }
Beispiel #9
0
    static void Main()
    {
        Duck RubberDuck = new RubberDuck();

        RubberDuck.PerformFly();
        RubberDuck.PerformQuack();
        RubberDuck.Display();
        Duck FredTheEagle = new Eagle();

        FredTheEagle.PerformFly();
        FredTheEagle.PerformQuack();
        FredTheEagle.Display();

        Console.ReadKey();
    }
Beispiel #10
0
        static void Main(string[] args)
        {
            Bird christianBird = new RubberDuck();

            christianBird.Display();
            christianBird.SetFlyBehavior(new YesFly());
            christianBird.PerformFly();
            christianBird.PerformQuack();
            christianBird.Swim();

            Bird mortyBird = new Swallow();

            mortyBird.Display();
            mortyBird.PerformFly();
            mortyBird.PerformQuack();
            mortyBird.Swim();
        }
Beispiel #11
0
        private static void RunDucks()
        {
            Duck duck            = null;
            bool exitApplication = false;

            do
            {
                Console.WriteLine("What do you want to see?\n1)DecoyDuck\n2)MallardDuck\n3)RubberDuck\nanykey to exit");
                Console.WriteLine(string.Empty);
                var response = Console.ReadLine();

                switch (response)
                {
                case "1":
                    duck = new DecoyDuck();
                    break;

                case "2":
                    duck = new MallardDuck();
                    break;

                case "3":
                    duck = new RubberDuck();
                    break;

                default:
                    exitApplication = true;
                    break;
                }
                if (!exitApplication)
                {
                    duck.Display();
                    duck.PerformFly();
                    duck.PerformQuack();
                }
                Console.WriteLine(string.Empty);
            }while(!exitApplication);
        }
Beispiel #12
0
        static void Main(string[] args)
        {
            var mallardDuck = new MallardDuck();

            mallardDuck.QuackBehavior = new QuackAsNormal();
            mallardDuck.FlyBehavior   = new FlyWithWings();
            Console.WriteLine($"Mallard duck {new string('-', 50)}");
            mallardDuck.Swim();
            mallardDuck.PerformQuack();
            mallardDuck.PerformFly();

            var redheadDuck = new RedheadDuck();

            redheadDuck.QuackBehavior = new QuackAsNormal();
            redheadDuck.FlyBehavior   = new FlyWithWings();
            Console.WriteLine($"Redhead duck {new string('-', 50)}");
            redheadDuck.Swim();
            redheadDuck.PerformQuack();
            redheadDuck.PerformFly();

            var rubberDuck = new RubberDuck();

            rubberDuck.QuackBehavior = new Sqeak();
            rubberDuck.FlyBehavior   = new FlyNoWay();
            Console.WriteLine($"Rubber duck {new string('-', 50)}");
            rubberDuck.Swim();
            rubberDuck.PerformQuack();
            rubberDuck.PerformFly();

            var decoyDuck = new DecoyDuck();

            decoyDuck.QuackBehavior = new Mute();
            decoyDuck.FlyBehavior   = new FlyNoWay();
            Console.WriteLine($"Decoy duck {new string('-', 50)}");
            decoyDuck.Swim();
            decoyDuck.PerformQuack();
            decoyDuck.PerformFly();
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            Console.WriteLine("##### 例子1 #####");

            // 无折扣.
            DiscountContext c1 = new DiscountContext(new NoDiscountStrategy());
            // 每本1元.
            DiscountContext c2 = new DiscountContext(new FlatRateStrategy()
            {
                OneCopyDiscount = 1
            });
            // 总金额 10%
            DiscountContext c3 = new DiscountContext(new PercentageStrategy()
            {
                Percent = 0.1M
            });

            Console.WriteLine(c1.GetBookDiscount(100, 10));
            Console.WriteLine(c2.GetBookDiscount(100, 10));
            Console.WriteLine(c3.GetBookDiscount(100, 10));



            Console.WriteLine("##### 例子2 #####");

            MallardDuck mallardDuck = new MallardDuck();
            RedheadDuck redheadDuck = new RedheadDuck();
            RubberDuck  rubberDuck  = new RubberDuck();

            Console.WriteLine(mallardDuck.Display() + " # " + mallardDuck.PerformFly().ToString() + " # " + mallardDuck.PerformQuack().ToString());
            Console.WriteLine(redheadDuck.Display() + " # " + redheadDuck.PerformFly().ToString() + " # " + redheadDuck.PerformQuack().ToString());
            Console.WriteLine(rubberDuck.Display() + " # " + rubberDuck.PerformFly().ToString() + " # " + rubberDuck.PerformQuack().ToString());



            Console.ReadLine();
        }
        public void RubberDuck_Should_Not_Fly()
        {
            var rubberDuck = new RubberDuck();

            Assert.Equal(String.Empty, rubberDuck.PerformFly());
        }
Beispiel #15
0
        static void Main(string[] args)
        {
            TestVirtual testVirtual = new TestVirtual();

            Console.WriteLine(testVirtual.resultA);         //using method from overrride
            Console.WriteLine(testVirtual.resultB);         //using method from base class

            Console.WriteLine(testVirtual.add(1, 2));       //using method from overrride
            Console.WriteLine(testVirtual.substract(9, 1)); //using method from base class
            Console.WriteLine();
            //======================================================================================

            SealMethod sealMethod = new SealMethod();

            Console.WriteLine(sealMethod.add(1, 1));
            Console.WriteLine(sealMethod.substract(2, 2));
            Console.WriteLine(sealMethod.multipy(2, 2));
            Console.WriteLine();
            //======================================================================================

            ProtectBase   protectBase   = new ProtectBase();
            TestProtected testProtected = new TestProtected();

            // below will have complier error as "add" method can only be used inside TestProtected
            //Console.WriteLine(TestProtected.add(4, 5));
            Console.WriteLine();
            //======================================================================================

            // below will have compier error - abstract can't be instantiate
            //AbstractBase ab = new AbstractBase();
            TestAbstract testAb = new TestAbstract();

            Console.WriteLine(testAb.add(3, 8));
            Console.WriteLine();
            //======================================================================================

            Console.WriteLine("---- test order system ----");
            Order order = new Order {
                Id = 1, FirstName = "Tan", Email = "*****@*****.**", Price = 10.50M
            };
            OrderingFactory factory = new OrderingFactory();
            IOrderProcess   orderA  = factory.GetOrderSupplier("A");

            orderA.MakeOrder(order);

            Console.WriteLine("");

            IOrderProcess orderB = factory.GetOrderSupplier("B");

            orderB.MakeOrder(order);

            Console.WriteLine("");
            Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++");
            Console.WriteLine("");
            Console.WriteLine(Multi10000(1, 1));
            Console.WriteLine();
            //======================================================================================
            IStore    store    = new Document();
            ICompress compress = new Document();

            store.Read();
            store.Write();
            compress.Compress();
            compress.Decompress();

            using (SimpleClass simple = new SimpleClass())
            {
                Console.WriteLine(simple.returnA());
            }
            Console.WriteLine();
            //======================================================================================


            Duck d1 = new DecoyDuck();

            d1.Display();
            d1.PerformFly();
            d1.PerformQuack();
            Duck d2 = new RubberDuck();

            d2.Display();
            d2.PerformFly();
            d2.PerformQuack();
            Console.WriteLine();
            //======================================================================================

            /*
             * WeatherData weatherData = new WeatherData();
             *
             * CurrentConditionsDisplay currDisplay = new CurrentConditionsDisplay(weatherData);
             * ForecastDisplay foreDisplay = new ForecastDisplay(weatherData);
             *
             * weatherData.setMeasurements(80, 65, 30.4f);
             */

            Console.WriteLine();
            //======================================================================================

            WeatherTracker provider = new WeatherTracker();

            CurrentConditionsDisplay currDisplay = new CurrentConditionsDisplay(provider);
            ForecastDisplay          foreDisplay = new ForecastDisplay(provider);

            provider.UpdateWeather(new WeatherData(80, 65, 30.4f));

            Console.WriteLine();
            //======================================================================================
            BeverageBase beverage = new Decaf();

            Console.WriteLine(beverage.Description
                              + " $" + beverage.cost());

            BeverageBase beverage2 = new DarkRoast();

            beverage2 = new Milk(beverage2);
            beverage2 = new Mocha(beverage2);
            Console.WriteLine(beverage2.Description
                              + " $" + beverage2.cost());


            Console.WriteLine();
            //======================================================================================
            Console.WriteLine(Singleton.Instant.getTest());
            Console.WriteLine(Singleton.Instant.getTest());
            Console.WriteLine(Singleton.Instant.getTest());


            Console.WriteLine();
            //======================================================================================
            Booking booking = new Booking {
                BookingId = 1, SupplierCode = "GTA", FirstName = "Tan", LastName = "Tang", TotalAmount = (decimal)299.99
            };
            ProcessBooking bookingProcess = new ProcessBooking(booking);

            bookingProcess.Initial();
            Console.WriteLine("  Booking State = {0}, Status = {1}", booking.State.ToString(), booking.Status);

            bookingProcess.PreAuthorisation();
            Console.WriteLine("  Booking State = {0}, Status = {1}", booking.State.ToString(), booking.Status);

            bookingProcess.MakeBooking();
            Console.WriteLine("  Booking State = {0}, Status = {1}", booking.State.ToString(), booking.Status);

            bookingProcess.MakePayment();
            Console.WriteLine("  Booking State = {0}, Status = {1}", booking.State.ToString(), booking.Status);

            bookingProcess.FinaliseBooking();
            Console.WriteLine("  Booking State = {0}, Status = {1}", booking.State.ToString(), booking.Status);


            Console.Read();
        }