static void Main(string[] args)
        {
            IFlyBehaviour flyNoWay     = new FlyNoWay();
            IFlyBehaviour flyWithWings = new FlyWithWings();
            Duck          mallardDuck  = new MallardDuck();

            mallardDuck.SetFlyBehaviour(flyWithWings);

            Duck redheadDuck = new RedheadDuck();

            redheadDuck.SetFlyBehaviour(flyWithWings);

            Duck rubberDuck = new RubberDuck();

            rubberDuck.SetFlyBehaviour(flyNoWay);

            Duck decoyDuck = new DecoyDuck();

            decoyDuck.SetFlyBehaviour(flyNoWay);


            List <Duck> listDucks = new List <Duck>
            {
                mallardDuck,
                redheadDuck,
                rubberDuck,
                decoyDuck
            };


            foreach (var duck in listDucks)
            {
                duck.Fly();
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine(" ** This is Duck Fly Learning Program from Head First Design Patterns \n Chap 1 - Strategy Pattern ** \n\n");

            Duck mallard = new MallardDuck();

            mallard.display();
            mallard.performQuack();
            mallard.performFly();

            Console.WriteLine();

            Duck redhead = new RedheadDuck();

            redhead.display();
            redhead.performQuack();
            redhead.performFly();

            Console.WriteLine();

            Duck rubber = new RubberDuck();

            rubber.display();
            rubber.performQuack();
            rubber.performFly();

            Console.WriteLine();
            Console.WriteLine("Adding Rocket Power to Rubber Duck");
            Console.WriteLine();

            // setting fly behavior for rubber ducky
            rubber.setFlyBehavior = new FlyRocketPowered();
            rubber.display();
            rubber.performFly();
        }
예제 #3
0
파일: Program.cs 프로젝트: ait27ru/SimUDuck
        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();
        }
예제 #4
0
        public void TestMethod1()
        {
            IFlyBehaviour flyBehaviour = new FlyBehaviour();

            MallardDuck mallardDuck = new MallardDuck(flyBehaviour);
            RedheadDuck redheadDuck = new RedheadDuck(flyBehaviour);

            IFlyBehaviour noFlyBehaviour = new NoFlyBehaviour();
            RubberDuck    rubberDuck     = new RubberDuck(noFlyBehaviour);


            mallardDuck.Display();
            mallardDuck.Fly();

            redheadDuck.Display();
            redheadDuck.Fly();

            rubberDuck.Display();
            rubberDuck.Fly();

            rubberDuck.ChangeFlyBehaviour(flyBehaviour);
            rubberDuck.Fly();

            redheadDuck.Swim();
            mallardDuck.Swim();
            rubberDuck.Swim();
        }
        public void RedheadDuck_Should_Quack_Swim_Fly_Display()
        {
            var redheadDuck = new RedheadDuck();

            Assert.Equal("quack", redheadDuck.PerformQuack());
            Assert.Equal("swim", redheadDuck.Swim());
            Assert.Equal("fly", redheadDuck.PerformFly());
            Assert.Equal("looks like a redhead", redheadDuck.Display());
        }
예제 #6
0
        public static void Main()
        {
            Duck redHeaDuck = new RedheadDuck("RedHead", "Red", new FlyWithRocket());

            redHeaDuck.Display();
            redHeaDuck.MakeItFly();

            Duck toyDuck = new ToyDuck("Toy", "Blue", new NoFly());

            toyDuck.Display();
            toyDuck.MakeItFly();
        }
예제 #7
0
        static void Main(string[] args)
        {
            // Strategy pattern
            IQuackable mallardDuck = new MallardDuck();
            IQuackable redheadDuck = new RedheadDuck();
            IQuackable rubberDuck  = new RubberDuck();

            duckCall(mallardDuck);
            duckCall(redheadDuck);
            duckCall(rubberDuck);

            Console.ReadKey();
        }
예제 #8
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();
        }
예제 #9
0
        static void Main()
        {
            Duck redhead = new RedheadDuck();

            redhead.DoFly();
            redhead.DoQuack();
            ToastDuck deadduck = new ToastDuck();

            deadduck.DoFly();
            deadduck.DoQuack();
            deadduck.SetFly(new FlyWithRocket());
            deadduck.DoFly();
        }
예제 #10
0
 // Use this for initialization
 void Start()
 {
     RedheadDuck red_duck = new RedheadDuck();
     red_duck.PerformFly();
     red_duck.PerformQuack();
     /*
     FANUC_OI_M test_class = new FANUC_OI_M();
     Debug.Log(test_class.G_Check("G01", 8));
     Debug.Log( test_class.M_Check("M03", 10));
     Debug.Log(test_class.ErrorMessage);
     Debug.Log(test_class.G_Check("M500", 19));
     Debug.Log(test_class.ErrorMessage);
     */
 }
예제 #11
0
        static void Main(string[] args)
        {
            // Strategy pattern
            IQuackable mallardDuck = new MallardDuck();
            IQuackable redheadDuck = new RedheadDuck();
            IQuackable rubberDuck  = new RubberDuck();

            // Adapter pattern
            IQuackable goose = new GooseAdapter(new Goose());

            duckCall(mallardDuck);
            duckCall(redheadDuck);
            duckCall(rubberDuck);
            duckCall(goose);

            Console.ReadKey();
        }
예제 #12
0
        static void Main(string[] args)
        {
            Duck duckMallard = new MallardDuck("José");
            Duck duckRedhead = new RedheadDuck("Jaum");
            Duck rubberDuck  = new RubberDuck("Pedro");

            duckMallard.Display();
            duckMallard.Quack();
            duckMallard.Swin();
            duckMallard.Fly();

            duckRedhead.Display();
            duckRedhead.Quack();
            duckRedhead.Swin();
            duckRedhead.Fly();

            rubberDuck.Display();
            rubberDuck.Quack();
            rubberDuck.Swin();
            rubberDuck.Fly();
        }
예제 #13
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    DbInitializer.Initialize(services);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }

            FlyWithWings fly   = new FlyWithWings();
            FlyNoWay     noFly = new FlyNoWay();

            MallardDuck md         = new MallardDuck(fly);
            RedheadDuck red        = new RedheadDuck(fly);
            RubberDuck  rubberDuck = new RubberDuck(noFly);

            md.Display();
            md.Fly();

            red.Display();
            red.Fly();

            rubberDuck.Display();
            rubberDuck.Fly();



            host.Run();
        }
예제 #14
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();
        }
예제 #15
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();
        }
예제 #16
0
        static void Main(string[] args)
        {
            MallardDuck mallardDuck = new MallardDuck();

            PlayWithDuck(mallardDuck);

            RedheadDuck redheadDuck = new RedheadDuck();

            PlayWithDuck(redheadDuck);

            RubberDuck rubberDuck = new RubberDuck();

            PlayWithDuck(rubberDuck);

            DecoyDuck decoyDuck = new DecoyDuck();

            PlayWithDuck(decoyDuck);

            ModelDuck modelDuck = new ModelDuck();

            PlayWithDuck(modelDuck);
            modelDuck.SetFly(FlyBehavior.FlyWithWings());
            PlayWithDuck(modelDuck);
        }