<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
狀態模式,當物件的內部狀態發生了改變的時候,允許物件執行不同的流程。
優點:
缺點:
這是一個狀態圖,具有 “有 25 分錢”、“沒有 25 分錢”、“售出糖果”、“糖果售罄” 這 4 個狀態。同時也對應 4 個動作:“投入 25 分錢”,“退回 25 分錢”,“轉動曲柄” 和 “發放糖果”。
class State: # 定義state基礎類別 def insert_quarter(self): pass def eject_quarter(self): pass def turn_crank(self): pass def dispense(self): pass class SoldOutState(State): # 繼承State 類 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): # 繼承State 類 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): # 繼承State 類 def __init__(self, gumball_machine): self.gumball_machine = gumball_machine def __str__(self): return "no_quarter" def insert_quarter(self): # 投幣 並且改變狀態 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): # 繼承State 類 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 # 找出所有狀態,並建立範例變數來持有當前狀態,然後定義狀態的值 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): # 投入25分錢 self.state.insert_quarter() def eject_quarter(self): # 退回25分 self.state.eject_quarter() # print("state", self.state, type(self.state)) def turn_crank(self): # 轉動曲柄 # print("state", self.state, type(self.state)) self.state.turn_crank() def release_ball(self): # 發放糖果 print("A gumball comes rolling out the slot...") if self.count > 0: self.count -= 1 if __name__ == "__main__": # 以下是程式碼測試 gumball_machine = GumballMachine(5) # 裝入5 個糖果 print(gumball_machine) gumball_machine.insert_quarter() # 投入25分錢 gumball_machine.turn_crank() # 轉動曲柄 print(gumball_machine) gumball_machine.insert_quarter() #投入25分錢 gumball_machine.eject_quarter() # 退錢 gumball_machine.turn_crank() # 轉動曲柄 print(gumball_machine) gumball_machine.insert_quarter() # 投入25分錢 gumball_machine.turn_crank() # 轉動曲柄 gumball_machine.insert_quarter() # 投入25分錢 gumball_machine.turn_crank() # 轉動曲柄 gumball_machine.eject_quarter() # 退錢 print(gumball_machine)
本篇文章就到這裡了,希望能夠給你帶來幫助,也希望您能夠多多關注it145.com的更多內容!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45