mirror of
https://github.com/rjNemo/design-patterns
synced 2026-06-06 02:26:40 +00:00
add code example
This commit is contained in:
parent
2be0019c86
commit
a4d2a99dda
4 changed files with 99 additions and 0 deletions
25
behavioral/state/concrete_states.py
Normal file
25
behavioral/state/concrete_states.py
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
"""
|
||||||
|
Concrete States implement various behaviors, associated with a state of the
|
||||||
|
Context.
|
||||||
|
"""
|
||||||
|
from behavioral.state.state import State
|
||||||
|
|
||||||
|
|
||||||
|
class ConcreteStateA(State):
|
||||||
|
def handle1(self) -> None:
|
||||||
|
print("ConcreteStateA handles request1.")
|
||||||
|
print("ConcreteStateA wants to change the state of the context.")
|
||||||
|
self.context.transition_to(ConcreteStateB())
|
||||||
|
|
||||||
|
def handle2(self) -> None:
|
||||||
|
print("ConcreteStateA handles request2.")
|
||||||
|
|
||||||
|
|
||||||
|
class ConcreteStateB(State):
|
||||||
|
def handle1(self) -> None:
|
||||||
|
print("ConcreteStateB handles request1.")
|
||||||
|
|
||||||
|
def handle2(self) -> None:
|
||||||
|
print("ConcreteStateB handles request2.")
|
||||||
|
print("ConcreteStateB wants to change the state of the context.")
|
||||||
|
self.context.transition_to(ConcreteStateA())
|
||||||
39
behavioral/state/context.py
Normal file
39
behavioral/state/context.py
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
from abc import ABC
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
class State(object):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Context(ABC):
|
||||||
|
"""
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# A reference to the current state of the Context.
|
||||||
|
_state: Optional[State] = None
|
||||||
|
|
||||||
|
def __init__(self, state: State) -> None:
|
||||||
|
self.transition_to(state)
|
||||||
|
|
||||||
|
def transition_to(self, state: State) -> None:
|
||||||
|
"""
|
||||||
|
The Context allows changing the State object at runtime.
|
||||||
|
"""
|
||||||
|
|
||||||
|
print(f"Context: Transition to {type(state).__name__ }")
|
||||||
|
self._state = state
|
||||||
|
self._state.context = self
|
||||||
|
|
||||||
|
"""
|
||||||
|
The Context delegates part of its behavior to the current State object.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def request1(self):
|
||||||
|
self._state.handle1()
|
||||||
|
|
||||||
|
def request2(self):
|
||||||
|
self._state.handle2()
|
||||||
7
behavioral/state/main.py
Normal file
7
behavioral/state/main.py
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# The client code.
|
||||||
|
from behavioral.state.concrete_states import ConcreteStateA
|
||||||
|
from behavioral.state.context import Context
|
||||||
|
|
||||||
|
context = Context(ConcreteStateA())
|
||||||
|
context.request1()
|
||||||
|
context.request2()
|
||||||
28
behavioral/state/state.py
Normal file
28
behavioral/state/state.py
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
from behavioral.state.context import Context
|
||||||
|
|
||||||
|
|
||||||
|
class State(ABC):
|
||||||
|
"""
|
||||||
|
The base State class declares methods that all Concrete State should
|
||||||
|
implement and also provides a backreference to the Context object,
|
||||||
|
associated with the State. This backreference can be used by States to
|
||||||
|
transition the Context to another State.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def context(self) -> Context:
|
||||||
|
return self._context
|
||||||
|
|
||||||
|
@context.setter
|
||||||
|
def context(self, context: Context) -> None:
|
||||||
|
self._context = context
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def handle1(self) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def handle2(self) -> None:
|
||||||
|
pass
|
||||||
Loading…
Reference in a new issue