예제 #1
0
        public static void Test_1()
        {
            var mallardDuck = new MallardDuck();

            mallardDuck.Display();
            mallardDuck.PerformFly();
            mallardDuck.SetFlyBehavior(new FlyNoWay());
            mallardDuck.PerformFly();
            mallardDuck.PerformQuack();
            mallardDuck.SetFlyBehavior(new FlyLikeRocket());
            mallardDuck.PerformFly();
        }
    // Use this for initialization
    void Start()
    {
        Duck millard = new MallardDuck();

        millard.PerformQuack();
        millard.PerformFly();

        millard.SetFlyBehavior(new FlyNoWay());
        millard.SetQuackBehavior(new MuteQuack());

        millard.PerformQuack();
        millard.PerformFly();
    }
        public void Duck_Behaviors_CanChangeBehaviorAtRuntime()
        {
            Duck mallard = new MallardDuck();

            mallard.Display();

            Assert.AreEqual("Quack!", mallard.Quack());
            Assert.AreEqual("I can fly!", mallard.Fly());

            mallard.SetQuackBehavior(new MuteQuack());
            mallard.SetFlyBehavior(new FlyNoWay());

            Assert.AreEqual("<silence>", mallard.Quack());
            Assert.AreEqual("I can't fly", mallard.Fly());
        }
예제 #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Create Mallard Duck");
            Console.WriteLine();

            Duck duck = new MallardDuck();

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

            Console.WriteLine("Change the state");
            Console.WriteLine();
            duck.SetFlyBehavior(new FlyRocketPowered());
            duck.SetQuackBehavior(new MuteQuack());
            duck.Display();
            duck.PerformQuack();
            duck.PerformFly();
            duck.Swim();
        }