-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq5.py
60 lines (44 loc) · 1.47 KB
/
q5.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
class DocumentState(object):
name = "state"
allowed = []
def switch(self, state):
""" Switch to new state """
if state.name in self.allowed:
print('Estado:', self, ' => documento agora está em', state.name)
self.__class__ = state
else:
print('Estado:', self, ' => Trocar para o estado', state.name, 'não é possivel.')
def __str__(self):
return self.name
class Draft(DocumentState):
name = "draft"
allowed = ['moderation', 'published']
class Moderation(DocumentState):
""" State of being powered on and working """
name = "moderation"
allowed = ['draft', 'published']
class Published(DocumentState):
""" State of being in suspended mode after switched on """
name = "published"
allowed = ['draft']
class Document(object):
""" A class representing a computer """
def __init__(self, autor='user'):
self.autor = autor
# State of the computer - default is off.
if self.autor == 'user':
self.state = Moderation()
elif self.autor == 'admin':
self.state = Published()
else:
raise ValueError(autor)
def change(self, state):
""" Change state """
self.state.switch(state)
doc = Document("admin")
doc.change(Draft)
doc.change(Moderation)
doc.change(Published)
doc.change(Moderation)
doc.change(Draft)
doc.change(Draft)