Пример #1
0
        private static void Main()
        {
            // Паттерн Стратегия!

            Duck mallard = new MallardDuck();

            // Вызов метода performQuack(), унаследованного классом MallardDuck;
            // метод делегирует выполнение операции по ссылке на QuackBehavior
            // (то есть вызывает quack() через унаследованную переменную quackBehavior)
            mallard.PerformQuack();
            // Затем то же самое происходит с методом performFly(), также унаследованным классом MallardDuck
            mallard.PerformFly();

            Duck model = new ModelDuck();

            // Первый вызов performFly() передаётся реализации, заданной в конструкторе...
            // ...ModelDuck - то есть экземпляру FlyNoWay
            model.PerformFly();
            // Утка-приманка вдруг взлетает на рекативном двигателе!
            model.FlyBehavior = new FlyRocketPowered();
            // Способность утки-приманки к полёту переключается динамически!
            // Если бы реализация находилась в иерархии Duck, такое было бы невозможно
            model.PerformFly();

            Console.Read();
        }
Пример #2
0
        static void Main(string[] args)
        {
            Duck mallard = new MallardDuck();
            mallard.PerformQuack();
            mallard.PerformFly();

            Duck modelDuck = new ModelDuck();
            modelDuck.PerformFly();
            modelDuck.FlyBehavior = new FlyRocketPowered();
            modelDuck.PerformFly();
        }
        static void Main(string[] args)
        {
            Duck mallard = new MallardDuck(); // Constructs new MallardDuck object

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

            Duck model = new ModelDuck();                 // Constructs new ModelDuck object

            model.performFly();                           // initially calls default Fly() method defined in ModelDuck subclass to set behavior
            model.setFlyBehavior(new FlyRocketPowered()); // Overrides default Fly() behavior
            model.performFly();
        }
Пример #4
0
        static void Main(string[] args)
        {
            Duck mallardDuck = new MallardDuck();

            mallardDuck.PerformQuack();
            mallardDuck.PerformFly();

            Duck model = new ModelDuck();

            model.PerformFly();
            model.setFlyBehavior(new FlyRocketPowered());
            model.PerformFly();
        }
Пример #5
0
        static void Main(string[] args)
        {
            Duck mallard = new MallardDuck();

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

            ModelDuck modelDuck = new ModelDuck();

            modelDuck.performFly();
            modelDuck.setFlyBehavior(new FlyRocketPowered());
            modelDuck.performFly();
            Console.ReadKey();
        }
Пример #6
0
        static void Main(string[] args)
        {
            Duck mallardDuck = new MallardDuck();

            mallardDuck.PerformFly();
            mallardDuck.PerformQuack();

            Console.WriteLine("------------------------------");

            Duck modelDuck = new ModelDuck();

            modelDuck.PerformFly();
            modelDuck.SetFlyBehavior(new FlyWithWings());
            modelDuck.PerformFly();
        }
Пример #7
0
        static void Main()
        {
            Duck mallard = new MallardDuck();
            mallard.PerformQuack();
            mallard.PerformFly();

            Duck model = new ModelDuck();
            model.PerformFly();
            model.FlyBehavior = new FlyRocketPowered();
            model.PerformFly();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
Пример #8
0
        public static void Main(string[] args)
        {
            Duck millardDuck = new MillardDuck();

            millardDuck.PerformFly();
            millardDuck.PerformQuack();

            Duck modelDuck = new ModelDuck();

            modelDuck.PerformFly();
            modelDuck.SetFlyBehavior(new FlyRocketPowered());
            modelDuck.PerformFly();

            Console.ReadLine();
        }
Пример #9
0
        public static void Main()
        {
            Duck mallard      = new MallardDuck();
            Duck rubberDuckie = new RubberDuck();
            Duck decoy        = new DecoyDuck();
            Duck model        = new ModelDuck();

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

            model.PerformFly();
            model.SetFlyBehavior(new FlyRocketPowered());
            model.PerformFly();
        }
Пример #10
0
        static void Main(string[] args)
        {
            Duck duck = new MallardDuck();

            duck.PerformFly();
            duck.PerformQuack();
            duck.Display();

            duck = new ModelDuck();
            duck.Display();
            duck.PerformFly();
            duck.PerformQuack();
            duck.SetFlyBehavior(new FlyWithRockets());
            duck.PerformFly();

            Console.ReadLine();
        }
Пример #11
0
        static void Main(string[] args)
        {
            Duck mallard = new MallardDuck();

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

            // No fly, mute
            Duck model = new ModelDuck(new FlyNoWay(), new MuteQuack());

            model.performQuack();
            model.performFly();

            // Change at runtime to quack and fly rocket powered
            model.setQuackBehavior(new Quack());
            model.setFlyBehavior(new FlyRocketPowered());
            model.performQuack();
            model.performFly();
        }
Пример #12
0
        static void Main()
        {
            /* // First example
            Context context;
            context = new Context(new ContreteStrategyA());
            context.ContextInterface();

            context = new Context(new ConcreateStrategyB());
            context.ContextInterface();

            // Second example
            SortedList studentRecords = new SortedList();

            studentRecords.Add("Samual");
            studentRecords.Add("Jimmy");
            studentRecords.Add("Sandra");
            studentRecords.Add("Vivek");
            studentRecords.Add("Anna");

            studentRecords.SetSortStrategy(new QuickSort());
            studentRecords.Sort();

            studentRecords.SetSortStrategy(new ShellSort());
            studentRecords.Sort();

            studentRecords.SetSortStrategy(new MergeSort());
            studentRecords.Sort();*/

            // Third example
            Duck m = new DuckFirst();
            m.performFly();
            m.performQuack();

            m = new ModelDuck();
            m.performFly();
            m.performQuack();

            // TDD example
            var reporter = new Reporter(new SimpleReportBuilder(), new SimpleReportSender());
            var reportCount = reporter.SendReports();
            Console.WriteLine(reportCount);
        }
Пример #13
0
        static void Main(string[] args)
        {
            Duck mallard = new MallardDuck();

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

            Console.WriteLine("");

            Duck model = new ModelDuck();

            model.Display();
            model.PerformFly();

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

            // Wait for user input
            Console.ReadKey();
        }
Пример #14
0
        static void Main(string[] args)
        {
            //Console.WriteLine("Strategy");

            //Duck duck = new RubberDuck();
            //duck.PerformFly();
            //duck.Display();

            //Duck duck = new MallardDuck();
            //duck.PerformFly();
            //duck.PerformQuack();
            //duck.Display();

            Duck model = new ModelDuck();

            model.PerformFly();

            model.SetFlyBehavior(new FlyRocketPowered());

            model.PerformFly();
        }
Пример #15
0
        static void Main(string[] args)
        {
            Duck mallarDuck = new MallarDuck();

            mallarDuck.Display();
            mallarDuck.PerformQuack();
            mallarDuck.PerformFly();

            Console.WriteLine();

            Duck modelDuck = new ModelDuck();

            modelDuck.Display();
            modelDuck.PerformQuack();
            modelDuck.PerformFly();
            //динамичекое изменение поведения во время выполнения
            modelDuck.SetFlyBehavior(new FlyRocketPowered());
            modelDuck.PerformFly();

            Console.ReadKey();
        }
Пример #16
0
        public static void Main()
        {

            Duck duck1 = new MallardDuck();
            Duck duck2 = new ModelDuck();

            duck1.Display();
            duck1.Swim();
            duck1.PerformQuack();
            duck1.PerformFly();

            Console.WriteLine();

            duck2.Display();
            duck2.Swim();
            duck2.PerformQuack();
            duck2.PerformFly();
            duck2.FlyBehavior = new FlyRocketPowered();
            duck2.PerformFly();


            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("*** Strategy ***\n");

            Console.WriteLine("=> Mallard Duck");
            Duck mallard = new MallardDuck();

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

            Console.WriteLine("=> Model Duck");
            Duck model = new ModelDuck();

            model.PerformFly();
            model.FlyBehavior = new FlyRockedPowered();
            model.PerformFly();
            Console.WriteLine();

#if (!vscode) // Add this for run from VS in order to console window will keep open
            Console.WriteLine("Press Enter for exit");
            Console.ReadLine();
#endif
        }
Пример #18
0
        static void Main(string[] args)
        {
            /*
             * The Strategy Pattern
             *
             * Defines a family of algorithms, encapsulates each one, and makes them interchangable.
             * Strategy lets the algorithm vary dependently from clients that use it.
             *
             * Solves problems:
             * -behaviors defined at compilation not runtime
             * -decereased code flexibility
             * -duplicate the code
             *
             * Allows:
             * -interchangeable algorithms for behavior for each object
             * -change behaviors at runtime
             * -common behaviors code can be reused as behaviours can be shared
             * -Favor composition over inheritance. (Design Principle #3)
             * -Program to an interface not implementation. (Design Principle #2)
             * -Identify the aspect of your code that vary and separate them from what stays the same.[Encapsulate What Varies] (Design Principle #1)
             *
             */

            //This is a Duck simulator of different kinds of ducks

            var mallard = new MallardDuck(); //can swim, quack, fly

            mallard.Display();
            mallard.Swim();
            mallard.Fly();
            mallard.Quack();

            Console.WriteLine();

            var redhead = new RedheadDuck(); //can swim, quack, fly

            redhead.Display();
            redhead.Swim();
            redhead.Fly();
            redhead.Quack();

            Console.WriteLine();

            var decoy = new DecoyDuck(); //can swim only

            decoy.Display();
            decoy.Swim();
            decoy.Fly();
            decoy.Quack();

            Console.WriteLine();

            var rubber = new RubberDuck(); //can swim and squeek, can't fly

            rubber.Display();
            rubber.Swim();
            rubber.Fly();
            rubber.Quack();

            Console.WriteLine();

            var model = new ModelDuck(); //can swim, can fake quack, can't fly unless defined

            model.Display();
            model.Swim();
            model.Fly();
            model.SetFlyBehavoir(new FlyRocketPowered());
            model.Fly();
            model.Quack();

            Console.ReadLine();
        }