-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
client.py
76 lines (54 loc) · 1.42 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# pylint: disable=too-few-public-methods
"The State Use Case Example"
from abc import ABCMeta, abstractmethod
class Context():
"This is the object whose behavior will change"
def __init__(self):
self.state_handles = [
Started(),
Running(),
Finished()
]
self._handle = iter(self.state_handles)
def request(self):
"Each time the request is called, a new class will handle it"
try:
self._handle.__next__()()
except StopIteration:
# resetting so it loops
self._handle = iter(self.state_handles)
class IState(metaclass=ABCMeta):
"A State Interface"
@staticmethod
@abstractmethod
def __call__():
"Set the default method"
class Started(IState):
"A ConcreteState Subclass"
@staticmethod
def method():
"A task of this class"
print("Task Started")
__call__ = method
class Running(IState):
"A ConcreteState Subclass"
@staticmethod
def method():
"A task of this class"
print("Task Running")
__call__ = method
class Finished(IState):
"A ConcreteState Subclass"
@staticmethod
def method():
"A task of this class"
print("Task Finished")
__call__ = method
# The Client
CONTEXT = Context()
CONTEXT.request()
CONTEXT.request()
CONTEXT.request()
CONTEXT.request()
CONTEXT.request()
CONTEXT.request()