コード例 #1
0
        static void Main(string[] args)
        {
            Duck myduck = new MallardDuck();

            myduck.display();
            myduck.performFly();
            myduck.performQuack();

            myduck = new RedHeadDuck();
            myduck.display();
            myduck.performFly();
            myduck.performQuack();

            myduck = new RubberDuck();
            myduck.display();
            myduck.performFly();
            myduck.performQuack();

            myduck = new DecoyDuck();
            myduck.display();
            myduck.performFly();
            myduck.performQuack();

            myduck.setFlyBehavior(new FlyJet());
            myduck.performFly();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Duck num1 = new MallardDuck();

            num1.display();
            num1.performFly();
            num1.performQuack();
            num1.Swim();

            Duck num2 = new RedheadDuck();

            num2.display();
            num2.performFly();
            num2.performQuack();
            num2.Swim();

            Duck num3 = new DecoyDuck();

            num3.display();
            num3.performFly();
            num3.performQuack();
            num3.Swim();
            num3.setFlyBehavior(new FlyWithWings());
            num3.performFly();

            Console.Read();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("We have four ducks available for the showcase.");
            Console.WriteLine("Choose one of these and type the number. ex) 1 = DecoyDuck");
            Console.WriteLine("");
            Console.WriteLine("1. DecoyDuck");
            Console.WriteLine("2. MallardDuck");
            Console.WriteLine("3. RedheadDuck");
            Console.WriteLine("4. RubberDuck");
            Console.WriteLine("");

            string end = String.Empty;

            do
            {
                Duck duck = default(Duck);

                string s = Console.ReadLine();
                if (!String.IsNullOrEmpty(s) && int.TryParse(s, out int result))
                {
                    switch (result)
                    {
                    case 1:
                    {
                        duck = new DecoyDuck();
                        duck.setFlyBehavior(new FlyNoWay());
                        duck.setQuackHeavior(new MuteQuack());
                    }
                    break;

                    case 2:
                    {
                        duck = new MallardDuck();
                        duck.setFlyBehavior(new FlyWithWings());
                        duck.setQuackHeavior(new StrategyPattern.Quack.Quack());
                    }
                    break;

                    case 3:
                    {
                        duck = new RedheadDuck();
                        duck.setFlyBehavior(new FlyWithWings());
                        duck.setQuackHeavior(new Squeak());
                    }
                    break;

                    case 4:
                    {
                        duck = new RubberDuck();
                        duck.setFlyBehavior(new FlyNoWay());
                        duck.setQuackHeavior(new MuteQuack());
                    }
                    break;

                    default:
                    {
                        Console.WriteLine("You choose a duck that is not on the list. I'm gonna just show you a rubber duck !");

                        duck = new RubberDuck();
                        duck.setFlyBehavior(new FlyNoWay());
                        duck.setQuackHeavior(new MuteQuack());
                    }
                    break;
                    }
                }
                else
                {
                    Console.WriteLine("Input only numbers. Don't type their names.");
                }

                duck.display();
                duck.performFly();
                duck.performQuack();
                duck.swim();

                end = Console.ReadLine();
            } while (end != "exit");
        }