-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatePattern.py
More file actions
136 lines (102 loc) · 3.29 KB
/
Copy pathStatePattern.py
File metadata and controls
136 lines (102 loc) · 3.29 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
State Pattern - Allows an object to change its behaviour when its internal state
changes. The object will seem to change its class as well.
Example - A candy dispenser is commonly found in shopping malls. This machine
takes in coin as an input and delivers candy as the output. There are states
involved in this process, hasCoin, noCoin, hasCandy and noCandy states
Each state implements the same set of methods but uses a different algorithm
to fit its use case.
"""
class CandyDispenser:
def __init__(self):
self.hasCoin = HasCoin(self)
self.noCoin = NoCoin(self)
self.hasCandy = HasCandy(self)
self.noCandy = NoCandy(self)
self.state = self.noCoin
self.candyCount = 1
def getHasCoin(self):
return self.hasCoin
def getNoCoin(self):
return self.noCoin
def getHasCandy(self):
return self.hasCandy
def getNoCandy(self):
return self.noCandy
def setState(self, state):
self.state = state
def dispense(self):
self.state.dispense()
def insertCoin(self):
self.state.insertCoin()
def ejectCoin(self):
self.state.ejectCoin()
def turnKnob(self):
self.state.turnKnob()
class State:
def __init__(self, dispenser):
self.dispenser = dispenser
def dispense(self):
print "You need to implement this method"
def insertCoin(self):
print "You need to implement this method"
def ejectCoin(self):
print "You need to implement this method"
def turnKnob(self):
print "You need to implement this method"
class HasCoin(State):
def dispense(self):
print "Nothing happens"
def insertCoin(self):
print "You have already got a coin inside"
def ejectCoin(self):
print "The machine returns you back your coin"
self.dispenser.setState(self.dispenser.getNoCoin())
def turnKnob(self):
print "Checking whether the machine is left with candies"
if self.dispenser.candyCount > 0:
self.dispenser.setState(self.dispenser.getHasCandy())
else:
self.dispenser.setState(self.dispenser.getNoCandy())
self.dispenser.dispense()
class NoCoin(State):
def dispense(self):
print "Nothing happens"
def insertCoin(self):
print "You inserted a coin"
self.dispenser.setState(self.dispenser.getHasCoin())
def ejectCoin(self):
print "There is no coin for you to eject"
def turnKnob(self):
print "Please don't try to cheat the system"
class HasCandy(State):
def dispense(self):
print "One candy drop out"
self.dispenser.candyCount = self.dispenser.candyCount - 1
print "Candies left: %d" % self.dispenser.candyCount
self.dispenser.setState(self.dispenser.getNoCoin())
def insertCoin(self):
print "Eating your coin, please wait"
def ejectCoin(self):
print "Sorry too late, eating your coin now"
def turnKnob(self):
print "You can't dispense twice!"
class NoCandy(State):
def dispense(self):
print "No more candy left, sorry"
self.dispenser.setState(self.dispenser.getHasCoin())
def insertCoin(self):
print "Your coin is still inside"
def ejectCoin(self):
print "The machine returns you back your coin"
self.dispenser.setState(self.dispenser.getNoCoin())
def turnKnob(self):
print "You can't turn the knob yet!"
candyDispenser = CandyDispenser()
candyDispenser.insertCoin()
candyDispenser.insertCoin()
candyDispenser.turnKnob()
candyDispenser.ejectCoin()
candyDispenser.insertCoin()
candyDispenser.turnKnob()
candyDispenser.ejectCoin()