Uh oh!
There was an error while loading. Please reload this page.
forked from faif/python-patterns
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.py
More file actions
Latest commit
89 lines (65 loc) · 2.08 KB
/
Copy pathstate.py
File metadata and controls
89 lines (65 loc) · 2.08 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Implementation of the state pattern
http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
*TL;DR
Implements state as a derived class of the state pattern interface.
Implements state transitions by invoking methods from the pattern's superclass.
"""
classState:
"""Base state. This is to share functionality"""
defscan(self):
"""Scan the dial to the next station"""
self.pos+=1
ifself.pos==len(self.stations):
self.pos=0
print("Scanning... Station is {} {}".format(self.stations[self.pos], self.name))
classAmState(State):
def__init__(self, radio):
self.radio=radio
self.stations= ["1250", "1380", "1510"]
self.pos=0
self.name="AM"
deftoggle_amfm(self):
print("Switching to FM")
self.radio.state=self.radio.fmstate
classFmState(State):
def__init__(self, radio):
self.radio=radio
self.stations= ["81.3", "89.1", "103.9"]
self.pos=0
self.name="FM"
deftoggle_amfm(self):
print("Switching to AM")
self.radio.state=self.radio.amstate
classRadio:
"""A radio. It has a scan button, and an AM/FM toggle switch."""
def__init__(self):
"""We have an AM state and an FM state"""
self.amstate=AmState(self)
self.fmstate=FmState(self)
self.state=self.amstate
deftoggle_amfm(self):
self.state.toggle_amfm()
defscan(self):
self.state.scan()
defmain():
"""
>>> radio = Radio()
>>> actions = [radio.scan] * 2 + [radio.toggle_amfm] + [radio.scan] * 2
>>> actions *= 2
>>> for action in actions:
... action()
Scanning... Station is 1380 AM
Scanning... Station is 1510 AM
Switching to FM
Scanning... Station is 89.1 FM
Scanning... Station is 103.9 FM
Scanning... Station is 81.3 FM
Scanning... Station is 89.1 FM
Switching to AM
Scanning... Station is 1250 AM
Scanning... Station is 1380 AM
"""
if__name__=="__main__":
importdoctest
doctest.testmod()