AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

Python 實現命令模式、中介者模式和解釋器模式

來源: 責編: 時間:2024-06-05 17:40:25 133觀看
導讀今天介紹三種行為型設計模式:命令模式、中介者模式和解釋器模式。1.命令模式它將請求封裝成一個對象,從而使得可以用不同的請求對客戶進行參數化。命令模式也支持撤銷操作。(1) 命令模式的結構命令模式的核心是命令對象

今天介紹三種行為型設計模式:命令模式、中介者模式和解釋器模式。fra28資訊網——每日最新資訊28at.com

fra28資訊網——每日最新資訊28at.com

1.命令模式

它將請求封裝成一個對象,從而使得可以用不同的請求對客戶進行參數化。命令模式也支持撤銷操作。fra28資訊網——每日最新資訊28at.com

(1) 命令模式的結構fra28資訊網——每日最新資訊28at.com

命令模式的核心是命令對象和接收者對象之間的關系。命令對象封裝了一個特定的請求,包含了執行該請求的方法。接收者對象負責實際執行請求。fra28資訊網——每日最新資訊28at.com

以下是命令模式的基本結構:fra28資訊網——每日最新資訊28at.com

# 命令對象接口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) 命令模式的應用場景fra28資訊網——每日最新資訊28at.com

命令模式適用于以下場景:fra28資訊網——每日最新資訊28at.com

  • 需要將請求的發送者和接收者解耦,使得它們可以獨立地變化。
  • 需要支持撤銷操作。

(3) 命令模式的優點fra28資訊網——每日最新資訊28at.com

  • 命令模式將請求的發送者和接收者解耦,使得它們可以獨立地變化。
  • 命令模式支持撤銷操作。
  • 命令模式遵循開閉原則,新的命令對象可以很容易地添加到系統中,而不會影響到原有的代碼。

(4) 命令模式的缺點fra28資訊網——每日最新資訊28at.com

  • 命令模式中,命令對象和接收者對象之間存在循環依賴的關系,可能會導致循環引用的問題。

2.中介者模式

它通過封裝一系列對象之間的交互,將對象之間的耦合度降低到最低。中介者模式將對象之間的交互轉移給中介者對象,從而使得對象之間不再直接相互引用。fra28資訊網——每日最新資訊28at.com

(1) 中介者模式的結構fra28資訊網——每日最新資訊28at.com

中介者模式的核心是中介者對象,它封裝了一系列對象之間的交互邏輯。中介者對象通常包含一個或多個接口,用于與其他對象進行通信。fra28資訊網——每日最新資訊28at.com

以下是中介者模式的基本結構:fra28資訊網——每日最新資訊28at.com

# 中介者接口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) 中介者模式的應用場景fra28資訊網——每日最新資訊28at.com

中介者模式適用于以下場景:fra28資訊網——每日最新資訊28at.com

  • 一組對象之間存在復雜的交互關系,導致對象之間的耦合度較高。
  • 要求對象之間的交互邏輯可以靈活地改變,而不需要修改對象之間的引用關系。

(3) 中介者模式的優點fra28資訊網——每日最新資訊28at.com

  • 中介者模式將對象之間的交互邏輯封裝到中介者對象中,從而使得對象之間的耦合度降低到最低。
  • 中介者模式使得對象之間的交互邏輯可以靈活地改變,而不需要修改對象之間的引用關系。
  • 中介者模式遵循開閉原則,新的同事類可以很容易地添加到系統中,而不會影響到原有的代碼。

(4) 中介者模式的缺點fra28資訊網——每日最新資訊28at.com

  • 中介者模式中,中介者對象通常需要知道所有的同事類,可能會導致中介者對象的職責過重。

3.解釋器模式

它定義了一種語言的文法,并解析相應的語句。解釋器模式通過定義語言的文法,將文法中的每個規則映射到一個類,然后通過遞歸的方式解析語句。fra28資訊網——每日最新資訊28at.com

(1) 解釋器模式的結構fra28資訊網——每日最新資訊28at.com

解釋器模式的核心是解釋器類,它封裝了解釋語句的邏輯。解釋器類通常包含一個或多個解釋方法,用于解釋語句的不同部分。fra28資訊網——每日最新資訊28at.com

以下是解釋器模式的基本結構:fra28資訊網——每日最新資訊28at.com

# 抽象表達式類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) 解釋器模式的應用場景fra28資訊網——每日最新資訊28at.com

解釋器模式適用于以下場景:fra28資訊網——每日最新資訊28at.com

  • 一種語言的文法比較簡單,且文法的規則可以通過類來表達。
  • 需要解析和執行一種特定的語言。

(3) 解釋器模式的優點fra28資訊網——每日最新資訊28at.com

  • 解釋器模式將解釋語句的邏輯封裝到解釋器類中,使得解釋語句的邏輯可以靈活地改變。
  • 解釋器模式遵循開閉原則,新的解釋器類可以很容易地添加到系統中,而不會影響到原有的代碼。

(4) 解釋器模式的缺點fra28資訊網——每日最新資訊28at.com

  • 解釋器模式中,解釋器類通常需要知道所有的語法規則,可能會導致解釋器類的職責過重。

本文鏈接:http://www.tebozhan.com/showinfo-26-92108-0.htmlPython 實現命令模式、中介者模式和解釋器模式

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: .NET 中的數據交互:生成和讀取YAML文件

下一篇: Vite 是什么(并且為什么如此流行)?

標簽:
  • 熱門焦點
Top