MCPcopy Create free account
hub / github.com/RefactoringGuru/design-patterns-python / Context

Class Context

src/State/Conceptual/main.py:18–60  ·  view source on GitHub ↗

EN: The Context defines the interface of interest to clients. It also maintains a reference to an instance of a State subclass, which represents the current state of the Context. RU: Контекст определяет интерфейс, представляющий интерес для клиентов. Он также хранит ссылку на э

Source from the content-addressed store, hash-verified

16
17
18class Context:
19 """
20 EN: The Context defines the interface of interest to clients. It also
21 maintains a reference to an instance of a State subclass, which represents
22 the current state of the Context.
23
24 RU: Контекст определяет интерфейс, представляющий интерес для клиентов. Он
25 также хранит ссылку на экземпляр подкласса Состояния, который отображает
26 текущее состояние Контекста.
27 """
28
29 _state = None
30 """
31 EN: A reference to the current state of the Context.
32
33 RU: Ссылка на текущее состояние Контекста.
34 """
35
36 def __init__(self, state: State) -> None:
37 self.transition_to(state)
38
39 def transition_to(self, state: State):
40 """
41 EN: The Context allows changing the State object at runtime.
42
43 RU: Контекст позволяет изменять объект Состояния во время выполнения.
44 """
45
46 print(f"Context: Transition to {type(state).__name__}")
47 self._state = state
48 self._state.context = self
49
50 """
51 EN: The Context delegates part of its behavior to the current State object.
52
53 RU: Контекст делегирует часть своего поведения текущему объекту Состояния.
54 """
55
56 def request1(self):
57 self._state.handle1()
58
59 def request2(self):
60 self._state.handle2()
61
62
63class State(ABC):

Callers 1

main.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected