The state pattern , When the internal state of an object changes , Allow objects to perform different processes .
advantage :
shortcoming :
This is a state diagram , have “ Yes 25 Cents ”、“ No, 25 Cents ”、“ Sell candy ”、“ Candy sold out ” this 4 Status . It also corresponds to 4 An action :“ input 25 Cents ”,“ return 25 Cents ”,“ Turn the crank ” and “ Give out candy ”.
class State:
# Definition state Base class
def insert_quarter(self):
pass
def eject_quarter(self):
pass
def turn_crank(self):
pass
def dispense(self):
pass
class SoldOutState(State):
# Inherit State class
def __init__(self, gumball_machine):
self.gumball_machine = gumball_machine
def __str__(self):
return "sold_out"
def insert_quarter(self):
print("You can't insert a quarter, the machine is sold out")
def eject_quarter(self):
print("You can't eject, you haven't inserted a quarter yet")
def turn_crank(self):
print("You turned, but ther are no gumballs")
def dispense(self):
print("No gumball dispensed")
class SoldState(State):
# Inherit State class
def __init__(self, gumball_machine):
self.gumball_machine = gumball_machine
def __str__(self):
return "sold"
def insert_quarter(self):
print("Please wait, we're already giving you a gumball")
def eject_quarter(self):
print("Sorry, you already turned the crank")
def turn_crank(self):
print("Turning twice doesn't get you another gumball")
def dispense(self):
self.gumball_machine.release_ball()
if gumball_machine.count > 0:
self.gumball_machine.state = self.gumball_machine.no_quarter_state
else:
print("Oops, out of gumballs!")
self.gumball_machine.state = self.gumball_machine.soldout_state
class NoQuarterState(State):
# Inherit State class
def __init__(self, gumball_machine):
self.gumball_machine = gumball_machine
def __str__(self):
return "no_quarter"
def insert_quarter(self):
# Coin-operated And change the State
print("You inserted a quarter")
self.gumball_machine.state = self.gumball_machine.has_quarter_state
def eject_quarter(self):
print("You haven't insert a quarter")
def turn_crank(self):
print("You turned, but there's no quarter")
def dispense(self):
print("You need to pay first")
class HasQuarterState(State):
# Inherit State class
def __init__(self, gumball_machine):
self.gumball_machine = gumball_machine
def __str__(self):
return "has_quarter"
def insert_quarter(self):
print("You can't insert another quarter")
def eject_quarter(self):
print("Quarter returned")
self.gumball_machine.state = self.gumball_machine.no_quarter_state
def turn_crank(self):
print("You turned...")
self.gumball_machine.state = self.gumball_machine.sold_state
def dispense(self):
print("No gumball dispensed")
class GumballMachine:
def __init__(self, count=0):
self.count = count
# Find all the States , And create instance variables to hold the current state , Then define the value of the State
self.soldout_state = SoldOutState(self)
self.no_quarter_state = NoQuarterState(self)
self.has_quarter_state = HasQuarterState(self)
self.sold_state = SoldState(self)
if count > 0:
self.state = self.no_quarter_state
else:
self.state = self.soldout_state
def __str__(self):
return ">>> Gumball machine current state: %s" % self.state
def insert_quarter(self):
# input 25 Cents
self.state.insert_quarter()
def eject_quarter(self):
# return 25 branch
self.state.eject_quarter()
# print("state", self.state, type(self.state))
def turn_crank(self):
# Turn the crank
# print("state", self.state, type(self.state))
self.state.turn_crank()
def release_ball(self):
# Give out candy
print("A gumball comes rolling out the slot...")
if self.count > 0:
self.count -= 1
if __name__ == "__main__":
# Here's the code test
gumball_machine = GumballMachine(5) # Load 5 A candy
print(gumball_machine)
gumball_machine.insert_quarter() # input 25 Cents
gumball_machine.turn_crank() # Turn the crank
print(gumball_machine)
gumball_machine.insert_quarter() # input 25 Cents
gumball_machine.eject_quarter() # Refund
gumball_machine.turn_crank() # Turn the crank
print(gumball_machine)
gumball_machine.insert_quarter() # input 25 Cents
gumball_machine.turn_crank() # Turn the crank
gumball_machine.insert_quarter() # input 25 Cents
gumball_machine.turn_crank() # Turn the crank
gumball_machine.eject_quarter() # Refund
print(gumball_machine)