Beispiel #1
0
        private static void PrintInfoForBehavioralPatterns()
        {
            // Chain Of Responsibility Test
            WriteTitleToConsole("Chain Of Responsibility Test");
            var random = new Random();
            var randomNumbers = Enumerable
                                .Repeat<Func<int, int, int>>(random.Next, 25)
                                .Select(rnd => rnd(0, 100))
                                .ToArray();
            Console.WriteLine(string.Join(",", randomNumbers));

            var algorithm = SortCombiner.GetSort();
            algorithm.ProcessSort(randomNumbers);
            Console.WriteLine(string.Join(",", randomNumbers));

            // C# Iterator Test
            WriteTitleToConsole("\nC# Iterator Test");
            var alphabet = new Alphabet("абвееегдежзийкабв");
            Console.WriteLine(string.Join(", ", alphabet));

            // Linked List Iterator Test
            WriteTitleToConsole("\nLinked List Iterator Test");
            var linkedList = new CustomList<int>();
            linkedList.Add(4);
            linkedList.Add(5);
            linkedList.Add(6);
            linkedList.Add(7);

            foreach (var item in linkedList)
            {
                Console.WriteLine(item);
            }

            linkedList.Clear();

            // Command Test
            WriteTitleToConsole("\nCommand Test");
            var engineer = new Engineer();
            engineer.Compute('+', 50);
            engineer.Compute('-', 10);
            engineer.Compute('*', 8);
            Console.WriteLine(engineer.Compute('/', 2));
            Console.WriteLine("Start Undo:");
            Console.WriteLine(engineer.Undo());
            Console.WriteLine(engineer.Undo());
            Console.WriteLine("Start Redo:");
            Console.WriteLine(engineer.Redo());
            Console.WriteLine(engineer.Redo());

            // Template Method Test
            WriteTitleToConsole("\nTemplate Method Test");
            var pepperoni = new Pepperoni();
            Console.WriteLine("Make pepperoni:");
            pepperoni.Make();

            var margherita = new Margherita();
            Console.WriteLine("\nMake margherita:");
            margherita.Make();

            // Strategy Test
            WriteTitleToConsole("\nStrategy Test");
            var general = new General(new List<IAttackStrategy>
                    {
                        new AirAttack(),
                        new LandAttack(),
                    });

            general.StartAttackOperation();

            // Observer Test
            WriteTitleToConsole("\nObserver Test");
            var school = RandomSchoolInitializer.getRandomSchool();
            var weakStudents = school.FoundAllWeakStudents().ToList();
            weakStudents.ForEach(student =>
            {
                student.Marks = new List<int>(student.Marks) { 2 };
            });

            // Mediator Test
            WriteTitleToConsole("\nMediator Test");
            var vivacom = new VivacomDispatcher();
            for (int i = 1; i <= 10; i++)
            {
                var client = new Client("Client" + (i).ToString(), i.ToString(), "Normal", DateTime.Now);
                vivacom.Register(client);
            }

            vivacom.Conect("4", "7");
            vivacom.Conect("5", "6");
            vivacom.Conect("1", "9");

            // Memento Test
            WriteTitleToConsole("\nMemento Test");
            var adder = new Adder("Bulgarian", 31, 23);
            var state = adder.getState();
            Console.WriteLine(adder.ToString());
            adder.Age += 11;
            adder.Length += 13.7;
            Console.WriteLine(adder.ToString());
            adder.RestorState(state);
            Console.WriteLine(adder.ToString());

            // State Test
            WriteTitleToConsole("\nState Test");
            var engine = new Engine(new StandardState());
            engine.Work();
            engine.SetNewState(new TurboState());
            engine.Work();

            // Interpreter Test (this example is from internet)
            WriteTitleToConsole("\nInterpreter Test (this example is from internet)");
            var input = "MCDLVII";
            var context = new Context(input);

            var tree = new List<Expression>();
            tree.Add(new ThousandExpression());
            tree.Add(new HundredExpression());
            tree.Add(new TenExpression());
            tree.Add(new OneExpression());

            foreach (var expression in tree)
            {
                expression.Interpret(context);
            }

            Console.WriteLine("{0} = {1}", input, context.Output);

            // Visitor Test
            WriteTitleToConsole("\nVisitor Test");
            var shopAssistant = new ShopAssistant()
            {
                Name = "John Doe",
                Age = 27,
                HealthCareFactor = 78,
                IsEmployed = true,
                VacantionDay = 15
            };

            shopAssistant.ShowInformation();
            shopAssistant.Accept(new HealthBonusesVisitor());
            shopAssistant.ShowInformation();
        }
Beispiel #2
0
 public override void Handle(Engine engine)
 {
     engine.Power = 300;
     engine.Depreciation = 10;
 }