コード例 #1
0
        static void Main(string[] args)
        {
            // Mallard Duck Example
            Console.WriteLine("Running the mallard duck example...");
            Duck mallard = new MallardDuck();

            mallard.PerformQuack();
            mallard.PerformFly();
            Console.WriteLine();

            // Model duck example that changes the duck's fly behavior at runtime
            // by calling its setter method for the fly behavior.
            Console.WriteLine("Running the model duck example...");
            Duck model = new ModelDuck();

            model.PerformQuack();

            // This first call to PerformFly() delegates to the flyBehavior object
            // set in the ModelDuck's constructor, which is a FlyNoWay instance.
            model.PerformFly();

            // To change a duck's behavior at runtime, just call the duck's setter
            // method for that behavior (see next example).

            // This invokes the model's inherited behavior setter method, and voila!
            // The model suddenly has rocket-powered flying capability!
            model.SetFlyBehavior(new FlyRocketPowered());

            // If it worked, the model duck dynamically changes its flying behavior!
            // You can't do that if the implementation lives inside the duck class.
            model.PerformFly();

            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Duck mallard = new ModelDuck();

            mallard.PerformQuack();
            mallard.PerformFly();
            mallard.SetFlyBehavior(new FlyRocketPowered());
            mallard.PerformFly();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            Duck mallard = new MallardDuck();

            mallard.PerformQuack();
            mallard.PerformFly();

            Duck model = new ModelDuck();

            model.PerformFly();
            model.SetFlyBehaviour(new FlyWithRocket());
            model.PerformFly();
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: garyng/design-patterns
        public void Run()
        {
            Duck mallardDuck = new MallardDuck();

            mallardDuck.Quack();
            mallardDuck.Fly();

            Duck modelDuck = new ModelDuck();

            modelDuck.Quack();
            modelDuck.Fly();
            Console.WriteLine("Changing model duck's flying behavior...");
            modelDuck.FlyBehavior = new FlyRocketPowered();
            modelDuck.Fly();
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: Ksyuint/design-patterns
        public static void Main(string[] args)
        {
            MallardDuck mallard      = new MallardDuck();
            RubberDuck  rubberDuckie = new RubberDuck();
            DecoyDuck   decoy        = new DecoyDuck();

            ModelDuck model = new ModelDuck();

            mallard.PerformQuack();
            rubberDuckie.PerformQuack();
            decoy.PerformQuack();

            model.PerformFly();
            model.FlyBehavior = new FlyRocketPowered();
            model.PerformFly();
        }