github地址:https://github.com/cheesezh/python_design_patternspython
用程序模擬,顧客直接向烤串師傅提需求。git
class Barbecuer(): def bake_mutton(self): print("烤羊肉串") def bake_chicken_wing(self): print("烤雞翅") def main(): boy = Barbecuer() boy.bake_mutton() boy.bake_mutton() boy.bake_mutton() boy.bake_chicken_wing() boy.bake_mutton() boy.bake_mutton() boy.bake_chicken_wing() main()
烤羊肉串 烤羊肉串 烤羊肉串 烤雞翅 烤羊肉串 烤羊肉串 烤雞翅
客戶端程序與「烤串師傅」緊耦合,儘管簡單,可是極爲僵化,當顧客多了,請求多了,就容易亂了。github
用程序模擬,顧客向服務員提需求,服務員再告知烤串師傅。app
from abc import ABCMeta, abstractmethod class Command(): """ 抽象命令類 """ __metaclass__ = ABCMeta # 須要肯定一個命令接收者 def __init__(self, receiver): self.receiver = receiver @abstractmethod def excute_command(self): pass class BakeMuttonCommand(Command): """ 具體命令類 """ def excute_command(self): self.receiver.bake_mutton() def to_string(self): return "烤羊肉串" class BakeChickenWingCommand(Command): """ 具體命令類 """ def excute_command(self): self.receiver.bake_chicken_wing() def to_string(self): return "烤雞翅" class Waiter(): """ 服務員類, 不用管顧客的烤串要怎麼烤,對於服務員來講,都看成命令記錄下來就行,而後通知「烤串師傅」執行便可。 """ def __init__(self): self.orders = [] def set_order(self, cmd): if cmd.to_string() == "烤雞翅": print("雞翅沒了,換點其餘的吧") else: self.orders.append(cmd) print("增長訂單:", cmd.to_string()) def cancel_order(self, cmd): self.orders.remove(cmd) print("取消訂單:", cmd.to_string()) def notify(self): for cmd in self.orders: cmd.excute_command() def main(): # 開店準備 boy = Barbecuer() bake_mutton_1 = BakeMuttonCommand(boy) bake_mutton_2 = BakeMuttonCommand(boy) bake_chicken_wing = BakeChickenWingCommand(boy) girl = Waiter() # 開門營業 girl.set_order(bake_mutton_1) girl.set_order(bake_mutton_2) girl.set_order(bake_chicken_wing) # 開始製做 girl.notify() main()
增長訂單: 烤羊肉串 增長訂單: 烤羊肉串 雞翅沒了,換點其餘的吧 烤羊肉串 烤羊肉串
命令模式,講一個請求封裝成一個對象,從而使你可用不一樣的請求對客戶進行參數化;對請求排隊或記錄請求日誌,以及支持可撤銷操做。[DP]設計
主要包括如下幾種類:日誌
命令模式的優勢:code