SHOW:
|
|
- or go back to the newest paste.
1 | using System; | |
2 | using System.Collections; | |
3 | using System.Collections.Generic; | |
4 | ||
5 | // Интерфейс стратегии вычисления | |
6 | public interface ICalculationStrategy | |
7 | { | |
8 | int Calculate(int prev1, int prev2); | |
9 | } | |
10 | ||
11 | // Конкретная стратегия вычисления квадрата разности | |
12 | public class SquareDifferenceStrategy : ICalculationStrategy | |
13 | { | |
14 | public int Calculate(int prev1, int prev2) | |
15 | { | |
16 | return (prev1 - prev2) * (prev1 - prev2); | |
17 | } | |
18 | } | |
19 | ||
20 | - | // Декоратор стратегии для изменения знака |
20 | + | |
21 | - | public class NegativeDecorator : ICalculationStrategy |
21 | + | |
22 | { | |
23 | private readonly int _x0; | |
24 | private readonly int _x1; | |
25 | - | public NegativeDecorator(ICalculationStrategy strategy) |
25 | + | |
26 | private readonly ICalculationStrategy _strategy; | |
27 | ||
28 | public Sequence(int x0, int x1, int n, ICalculationStrategy strategy) | |
29 | { | |
30 | _x0 = x0; | |
31 | _x1 = x1; | |
32 | - | return -_strategy.Calculate(prev1, prev2); |
32 | + | |
33 | _strategy = strategy; | |
34 | } | |
35 | ||
36 | // Внутренний итератор с использованием yield return | |
37 | public IEnumerator<int> GetEnumerator() | |
38 | { | |
39 | if (_n <= 0) yield break; | |
40 | ||
41 | // Возвращаем первый элемент | |
42 | yield return _x0; | |
43 | if (_n == 1) yield break; | |
44 | ||
45 | // Возвращаем второй элемент | |
46 | yield return _x1; | |
47 | if (_n == 2) yield break; | |
48 | ||
49 | int prev2 = _x0; | |
50 | int prev1 = _x1; | |
51 | ||
52 | // Генерируем остальные элементы последовательности | |
53 | for (int i = 2; i < _n; i++) | |
54 | { | |
55 | int current = _strategy.Calculate(prev1, prev2); | |
56 | yield return current; | |
57 | prev2 = prev1; | |
58 | prev1 = current; | |
59 | } | |
60 | } | |
61 | ||
62 | IEnumerator IEnumerable.GetEnumerator() | |
63 | { | |
64 | return GetEnumerator(); | |
65 | } | |
66 | } | |
67 | ||
68 | // Дополнительная стратегия суммирования | |
69 | public class SumStrategy : ICalculationStrategy | |
70 | { | |
71 | public int Calculate(int prev1, int prev2) | |
72 | { | |
73 | return prev1 + prev2; | |
74 | } | |
75 | } | |
76 | ||
77 | class Program | |
78 | { | |
79 | static void Main() | |
80 | { | |
81 | // Тест базовой последовательности | |
82 | Console.WriteLine("Original sequence:"); | |
83 | var sequence = new Sequence(0, 2, 10, new SquareDifferenceStrategy()); | |
84 | foreach (var number in sequence) | |
85 | { | |
86 | Console.Write($"{number} "); | |
87 | } | |
88 | Console.WriteLine(); | |
89 | ||
90 | // Демонстрация работы с другой стратегией (числа Фибоначчи) | |
91 | Console.WriteLine("\nFibonacci sequence:"); | |
92 | var fibonacciSequence = new Sequence(0, 1, 10, new SumStrategy()); | |
93 | - | // Дополнительный декоратор умножения на 2 |
93 | + | |
94 | - | public class MultiplyByTwoDecorator : ICalculationStrategy |
94 | + | |
95 | Console.Write($"{number} "); | |
96 | } | |
97 | Console.WriteLine(); | |
98 | - | public MultiplyByTwoDecorator(ICalculationStrategy strategy) |
98 | + | |
99 | } | |
100 |