EN: The Context defines the interface of interest to clients. RU: Контекст определяет интерфейс, представляющий интерес для клиентов.
| 18 | |
| 19 | |
| 20 | class Context(): |
| 21 | """ |
| 22 | EN: The Context defines the interface of interest to clients. |
| 23 | |
| 24 | RU: Контекст определяет интерфейс, представляющий интерес для клиентов. |
| 25 | """ |
| 26 | |
| 27 | def __init__(self, strategy: Strategy) -> None: |
| 28 | """ |
| 29 | EN: Usually, the Context accepts a strategy through the constructor, but |
| 30 | also provides a setter to change it at runtime. |
| 31 | |
| 32 | RU: Обычно Контекст принимает стратегию через конструктор, а также |
| 33 | предоставляет сеттер для её изменения во время выполнения. |
| 34 | """ |
| 35 | |
| 36 | self._strategy = strategy |
| 37 | |
| 38 | @property |
| 39 | def strategy(self) -> Strategy: |
| 40 | """ |
| 41 | EN: The Context maintains a reference to one of the Strategy objects. |
| 42 | The Context does not know the concrete class of a strategy. It should |
| 43 | work with all strategies via the Strategy interface. |
| 44 | |
| 45 | RU: Контекст хранит ссылку на один из объектов Стратегии. Контекст не |
| 46 | знает конкретного класса стратегии. Он должен работать со всеми |
| 47 | стратегиями через интерфейс Стратегии. |
| 48 | """ |
| 49 | |
| 50 | return self._strategy |
| 51 | |
| 52 | @strategy.setter |
| 53 | def strategy(self, strategy: Strategy) -> None: |
| 54 | """ |
| 55 | EN: Usually, the Context allows replacing a Strategy object at runtime. |
| 56 | |
| 57 | RU: Обычно Контекст позволяет заменить объект Стратегии во время |
| 58 | выполнения. |
| 59 | """ |
| 60 | |
| 61 | self._strategy = strategy |
| 62 | |
| 63 | def do_some_business_logic(self) -> None: |
| 64 | """ |
| 65 | EN: The Context delegates some work to the Strategy object instead of |
| 66 | implementing multiple versions of the algorithm on its own. |
| 67 | |
| 68 | RU: Вместо того, чтобы самостоятельно реализовывать множественные версии |
| 69 | алгоритма, Контекст делегирует некоторую работу объекту Стратегии. |
| 70 | """ |
| 71 | |
| 72 | # ... |
| 73 | |
| 74 | print("Context: Sorting data using the strategy (not sure how it'll do it)") |
| 75 | result = self._strategy.do_algorithm(["a", "b", "c", "d", "e"]) |
| 76 | print(",".join(result)) |
| 77 |