コード例 #1
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();
        }
コード例 #2
0
        public void Run()
        {
            Console.WriteLine("Program to an interface, not an implementation");

            RubberDuck      rubberDuck      = new RubberDuck();
            IFlyBehaviour   flyWithRocket   = new FlyWithRocket();
            IQuackBehaviour quackWithSqueek = new QuackWithSqueek();

            rubberDuck.setFlyBehaivour(flyWithRocket);
            rubberDuck.setQuackBehaivour(quackWithSqueek);
            rubberDuck.performFly();
            rubberDuck.performQuack();
        }