今天介紹三種行為型設計模式:命令模式、中介者模式和解釋器模式。
它將請求封裝成一個對象,從而使得可以用不同的請求對客戶進行參數化。命令模式也支持撤銷操作。
(1) 命令模式的結構
命令模式的核心是命令對象和接收者對象之間的關系。命令對象封裝了一個特定的請求,包含了執行該請求的方法。接收者對象負責實際執行請求。
以下是命令模式的基本結構:
# 命令對象接口class Command: def execute(self): pass def undo(self): pass# 具體命令對象類class ConcreteCommandA(Command): def __init__(self, receiver): self.receiver = receiver def execute(self): self.receiver.action_a() def undo(self): self.receiver.undo_action_a()class ConcreteCommandB(Command): def __init__(self, receiver): self.receiver = receiver def execute(self): self.receiver.action_b() def undo(self): self.receiver.undo_action_b()# 接收者對象類class Receiver: def action_a(self): print("接收者執行動作A") def action_b(self): print("接收者執行動作B") def undo_action_a(self): print("接收者撤銷動作A") def undo_action_b(self): print("接收者撤銷動作B")# 客戶端代碼if __name__ == "__main__": receiver = Receiver() command_a = ConcreteCommandA(receiver) command_b = ConcreteCommandB(receiver) invoker = Invoker() invoker.set_command(command_a) invoker.execute_command() invoker.set_command(command_b) invoker.execute_command()
(2) 命令模式的應用場景
命令模式適用于以下場景:
(3) 命令模式的優點
(4) 命令模式的缺點
它通過封裝一系列對象之間的交互,將對象之間的耦合度降低到最低。中介者模式將對象之間的交互轉移給中介者對象,從而使得對象之間不再直接相互引用。
(1) 中介者模式的結構
中介者模式的核心是中介者對象,它封裝了一系列對象之間的交互邏輯。中介者對象通常包含一個或多個接口,用于與其他對象進行通信。
以下是中介者模式的基本結構:
# 中介者接口class Mediator: def send(self, message, colleague): pass# 同事類接口class Colleague: def set_mediator(self, mediator): pass def send(self, message): pass def receive(self, message): pass# 具體中介者類class ConcreteMediator(Mediator): def __init__(self): self.colleague_a = None self.colleague_b = None def set_colleague_a(self, colleague_a): self.colleague_a = colleague_a def set_colleague_b(self, colleague_b): self.colleague_b = colleague_b def send(self, message, colleague): if colleague == self.colleague_a: self.colleague_b.receive(message) elif colleague == self.colleague_b: self.colleague_a.receive(message)# 具體同事類class ConcreteColleagueA(Colleague): def __init__(self, mediator): self.mediator = mediator def set_mediator(self, mediator): self.mediator = mediator def send(self, message): self.mediator.send(message, self) def receive(self, message): print("同事A收到消息:", message)class ConcreteColleagueB(Colleague): def __init__(self, mediator): self.mediator = mediator def set_mediator(self, mediator): self.mediator = mediator def send(self, message): self.mediator.send(message, self) def receive(self, message): print("同事B收到消息:", message)# 客戶端代碼if __name__ == "__main__": mediator = ConcreteMediator() colleague_a = ConcreteColleagueA(mediator) colleague_b = ConcreteColleagueB(mediator) mediator.set_colleague_a(colleague_a) mediator.set_colleague_b(colleague_b) colleague_a.send("Hello, colleague B!") colleague_b.send("Hi, colleague A!")
(2) 中介者模式的應用場景
中介者模式適用于以下場景:
(3) 中介者模式的優點
(4) 中介者模式的缺點
它定義了一種語言的文法,并解析相應的語句。解釋器模式通過定義語言的文法,將文法中的每個規則映射到一個類,然后通過遞歸的方式解析語句。
(1) 解釋器模式的結構
解釋器模式的核心是解釋器類,它封裝了解釋語句的邏輯。解釋器類通常包含一個或多個解釋方法,用于解釋語句的不同部分。
以下是解釋器模式的基本結構:
# 抽象表達式類class AbstractExpression: def interpret(self, context): pass# 終結符表達式類class TerminalExpression(AbstractExpression): def interpret(self, context): # 解釋終結符表達式的邏輯 pass# 非終結符表達式類class NonterminalExpression(AbstractExpression): def __init__(self): self.expressions = [] def add_expression(self, expression): self.expressions.append(expression) def interpret(self, context): # 解釋非終結符表達式的邏輯 for expression in self.expressions: expression.interpret(context)# 上下文類class Context: def __init__(self): self.input = None self.output = None# 客戶端代碼if __name__ == "__main__": context = Context() # 構建語法樹 expression1 = TerminalExpression() expression2 = NonterminalExpression() expression3 = TerminalExpression() expression2.add_expression(expression1) expression2.add_expression(expression3) # 解釋語句 expression2.interpret(context)
(2) 解釋器模式的應用場景
解釋器模式適用于以下場景:
(3) 解釋器模式的優點
(4) 解釋器模式的缺點
本文鏈接:http://www.tebozhan.com/showinfo-26-92108-0.htmlPython 實現命令模式、中介者模式和解釋器模式
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
下一篇: Vite 是什么(并且為什么如此流行)?