资讯动态

设计模式之行为型设计模式详解

发布时间:2026/8/17 13:42:18 来源:尧图企业网站定制
行为型模式关注对象之间的通信和职责分配定义了对象间的交互方式和算法的封装。目录责任链模式 (Chain of Responsibility)命令模式 (Command)解释器模式 (Interpreter)迭代器模式 (Iterator)中介者模式 (Mediator)备忘录模式 (Memento)观察者模式 (Observer)状态模式 (State)策略模式 (Strategy)模板方法模式 (Template Method)访问者模式 (Visitor)1. 责任链模式 (Chain of Responsibility)定义使多个对象都有机会处理请求从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链并沿着这条链传递请求直到有一个对象处理它为止。生活类比公司报销审批员工提交报销单先给组长审批额度超限则转给经理再超限转给总监技术支持热线一级客服解决不了转二级二级解决不了转专家Python 实现from abc import ABC, abstractmethod from typing import Optional class Handler(ABC): def __init__(self): self._successor: Optional[Handler] None def set_successor(self, successor: Handler): self._successor successor abstractmethod def handle_request(self, amount: float): pass class TeamLeader(Handler): def handle_request(self, amount: float): if amount 1000: print(f组长批准了 {amount} 元的报销) elif self._successor: print(f组长无法批准 {amount} 元转交给上级) self._successor.handle_request(amount) class Manager(Handler): def handle_request(self, amount: float): if amount 5000: print(f经理批准了 {amount} 元的报销) elif self._successor: print(f经理无法批准 {amount} 元转交给上级) self._successor.handle_request(amount) class Director(Handler): def handle_request(self, amount: float): if amount 10000: print(f总监批准了 {amount} 元的报销) else: print(f总监{amount} 元超出权限需要董事会决议) if __name__ __main__: leader TeamLeader() manager Manager() director Director() leader.set_successor(manager) manager.set_successor(director) expenses [800, 3000, 7000, 15000] for expense in expenses: print(f\n申请报销: {expense} 元) leader.handle_request(expense)2. 命令模式 (Command)定义将一个请求封装为一个对象从而使你可用不同的请求对客户进行参数化对请求排队或记录请求日志以及支持可撤销的操作。生活类比餐厅点餐服务员调用者记录订单命令厨师接收者执行遥控器按钮命令控制各种家电接收者可以记录、撤销操作Python 实现from abc import ABC, abstractmethod from typing import List class Command(ABC): abstractmethod def execute(self): pass abstractmethod def undo(self): pass class Light: def __init__(self, location: str): self.location location self.is_on False def on(self): self.is_on True print(f{self.location} 灯已打开) def off(self): self.is_on False print(f{self.location} 灯已关闭) class LightOnCommand(Command): def __init__(self, light: Light): self.light light self.previous_state False def execute(self): self.previous_state self.light.is_on self.light.on() def undo(self): if not self.previous_state: self.light.off() class RemoteControl: def __init__(self): self.commands: dict {} self.history: List[Command] [] def set_command(self, slot: str, command: Command): self.commands[slot] command def press_button(self, slot: str): if slot in self.commands: command self.commands[slot] command.execute() self.history.append(command) def press_undo(self): if self.history: command self.history.pop() print(\n撤销上一个操作:) command.undo() if __name__ __main__: remote RemoteControl() living_room_light Light(客厅) remote.set_command(A, LightOnCommand(living_room_light)) remote.press_button(A) remote.press_undo()3. 解释器模式 (Interpreter)定义给定一个语言定义它的文法的一种表示并定义一个解释器这个解释器使用该表示来解释语言中的句子。生活类比翻译软件将一种语言的句子解析并转换成另一种语言计算器解析数学表达式并计算结果正则表达式定义匹配规则并解释执行Python 实现from abc import ABC, abstractmethod from typing import Dict class Context: def __init__(self): self.variables: Dict[str, int] {} def set_variable(self, name: str, value: int): self.variables[name] value def get_variable(self, name: str) - int: return self.variables.get(name, 0) class Expression(ABC): abstractmethod def interpret(self, context: Context) - int: pass class NumberExpression(Expression): def __init__(self, value: int): self.value value def interpret(self, context: Context) - int: return self.value class VariableExpression(Expression): def __init__(self, name: str): self.name name def interpret(self, context: Context) - int: return context.get_variable(self.name) class AddExpression(Expression): def __init__(self, left: Expression, right: Expression): self.left left self.right right def interpret(self, context: Context) - int: return self.left.interpret(context) self.right.interpret(context) if __name__ __main__: context Context() context.set_variable(x, 10) context.set_variable(y, 5) # (x y) * 2 expression AddExpression( VariableExpression(x), VariableExpression(y) ) result expression.interpret(context) print(fx y {result})4. 迭代器模式 (Iterator)定义提供一种方法顺序访问一个聚合对象中各个元素而又不需暴露该对象的内部表示。生活类比电视遥控器换台按上下键遍历频道不需要知道电视台内部如何存储翻书一页一页翻看不需要知道书是如何装订的Python 实现from abc import ABC, abstractmethod from typing import List, Any class Iterator(ABC): abstractmethod def has_next(self) - bool: pass abstractmethod def next(self) - Any: pass class Book: def __init__(self, title: str): self.title title class BookShelf: def __init__(self): self._books: List[Book] [] def add_book(self, book: Book): self._books.append(book) def create_iterator(self): return BookShelfIterator(self) def get_book_at(self, index: int) - Book: return self._books[index] def get_length(self) - int: return len(self._books) class BookShelfIterator(Iterator): def __init__(self, book_shelf: BookShelf): self._book_shelf book_shelf self._index 0 def has_next(self) - bool: return self._index self._book_shelf.get_length() def next(self) - Book: book self._book_shelf.get_book_at(self._index) self._index 1 return book if __name__ __main__: book_shelf BookShelf() book_shelf.add_book(Book(设计模式)) book_shelf.add_book(Book(Clean Code)) iterator book_shelf.create_iterator() while iterator.has_next(): book iterator.next() print(fBook: {book.title})5. 中介者模式 (Mediator)定义用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用从而使其耦合松散。生活类比机场塔台飞机之间不直接通信而是通过塔台协调起降聊天室用户通过聊天室服务器转发消息而不是直接发送给每个人Python 实现from abc import ABC, abstractmethod from typing import List class Mediator(ABC): abstractmethod def send_message(self, message: str, colleague: Colleague): pass class Colleague(ABC): def __init__(self, name: str, mediator: Mediator): self.name name self._mediator mediator def send(self, message: str): self._mediator.send_message(message, self) abstractmethod def receive_message(self, message: str): pass class ChatRoom(Mediator): def __init__(self): self._participants: List[Colleague] [] def register(self, colleague: Colleague): self._participants.append(colleague) def send_message(self, message: str, sender: Colleague): for participant in self._participants: if participant ! sender: participant.receive_message(f[{sender.name}]: {message}) class User(Colleague): def receive_message(self, message: str): print(f{self.name} 收到: {message}) if __name__ __main__: chat_room ChatRoom() alice User(Alice, chat_room) bob User(Bob, chat_room) chat_room.register(alice) chat_room.register(bob) alice.send(Hello!)6. 备忘录模式 (Memento)定义在不破坏封装性的前提下捕获一个对象的内部状态并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。生活类比游戏存档保存游戏进度可以随时读档回到之前的状态CtrlZ撤销编辑器保存历史状态支持撤销操作Python 实现from abc import ABC, abstractmethod from typing import List from datetime import datetime class Memento: def __init__(self, content: str, cursor_position: int): self._content content self._cursor_position cursor_position self._timestamp datetime.now().strftime(%Y-%m-%d %H:%M:%S) def get_content(self) - str: return self._content def get_timestamp(self) - str: return self._timestamp class TextEditor: def __init__(self): self._content self._cursor_position 0 def type(self, text: str): self._content text print(f输入: {text} - {self._content}) def create_memento(self) - Memento: return Memento(self._content, self._cursor_position) def restore_memento(self, memento: Memento): self._content memento.get_content() print(f恢复: {self._content}) class History: def __init__(self): self._history: List[Memento] [] def backup(self, memento: Memento): self._history.append(memento) def undo(self) - Memento: if len(self._history) 1: self._history.pop() return self._history[-1] return None if __name__ __main__: editor TextEditor() history History() history.backup(editor.create_memento()) editor.type(Hello) history.backup(editor.create_memento()) editor.type( World) memento history.undo() if memento: editor.restore_memento(memento)7. 观察者模式 (Observer)定义定义对象间的一种一对多的依赖关系当一个对象的状态发生改变时所有依赖于它的对象都得到通知并被自动更新。生活类比微信公众号订阅者收到新文章推送天气预报多个显示面板同步更新天气数据Python 实现from abc import ABC, abstractmethod from typing import List class Observer(ABC): abstractmethod def update(self, temperature: float, humidity: float): pass class Subject(ABC): abstractmethod def attach(self, observer: Observer): pass abstractmethod def notify(self): pass class WeatherStation(Subject): def __init__(self): self._observers: List[Observer] [] self._temperature 0.0 def attach(self, observer: Observer): self._observers.append(observer) def notify(self): for observer in self._observers: observer.update(self._temperature, 0.0) def set_temperature(self, temperature: float): self._temperature temperature self.notify() class PhoneDisplay(Observer): def update(self, temperature: float, humidity: float): print(fPhone: Temperature is {temperature}) if __name__ __main__: weather WeatherStation() phone PhoneDisplay() weather.attach(phone) weather.set_temperature(25.0)8. 状态模式 (State)定义允许对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类。生活类比电梯有停止、运行、开门、关门等状态每种状态下行为不同红绿灯红灯停、绿灯行、黄灯等待Python 实现from abc import ABC, abstractmethod class State(ABC): abstractmethod def handle(self, context: TrafficLight): pass class RedState(State): def handle(self, context: TrafficLight): print(Red: Stop!) context.set_state(GreenState()) class GreenState(State): def handle(self, context: TrafficLight): print(Green: Go!) context.set_state(YellowState()) class YellowState(State): def handle(self, context: TrafficLight): print(Yellow: Prepare to stop) context.set_state(RedState()) class TrafficLight: def __init__(self): self._state RedState() def set_state(self, state: State): self._state state def change(self): self._state.handle(self) if __name__ __main__: light TrafficLight() for _ in range(6): light.change()9. 策略模式 (Strategy)定义定义一系列的算法把它们一个个封装起来并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。生活类比导航软件可以选择最快路线、最短路线、避开高速等策略支付方式支付宝、微信、信用卡等不同支付策略Python 实现from abc import ABC, abstractmethod from typing import List class SortStrategy(ABC): abstractmethod def sort(self, data: List[int]) - List[int]: pass class BubbleSortStrategy(SortStrategy): def sort(self, data: List[int]) - List[int]: arr data.copy() n len(arr) for i in range(n): for j in range(0, n - i - 1): if arr[j] arr[j 1]: arr[j], arr[j 1] arr[j 1], arr[j] return arr class QuickSortStrategy(SortStrategy): def sort(self, data: List[int]) - List[int]: if len(data) 1: return data pivot data[len(data) // 2] left [x for x in data if x pivot] middle [x for x in data if x pivot] right [x for x in data if x pivot] return self.sort(left) middle self.sort(right) class Sorter: def __init__(self, strategy: SortStrategy None): self._strategy strategy or QuickSortStrategy() def set_strategy(self, strategy: SortStrategy): self._strategy strategy def sort(self, data: List[int]) - List[int]: return self._strategy.sort(data) if __name__ __main__: data [64, 34, 25, 12, 22, 11, 90] sorter Sorter() sorter.set_strategy(BubbleSortStrategy()) print(fBubble: {sorter.sort(data)}) sorter.set_strategy(QuickSortStrategy()) print(fQuick: {sorter.sort(data)})10. 模板方法模式 (Template Method)定义定义一个操作中的算法的骨架而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。生活类比冲泡饮料泡茶和泡咖啡的步骤类似烧水、冲泡、倒入杯中、加调料但具体实现不同建造房屋建造流程固定打地基、建墙体、盖屋顶但材料不同Python 实现from abc import ABC, abstractmethod class CaffeineBeverage(ABC): def prepare_recipe(self): self.boil_water() self.brew() self.pour_in_cup() self.add_condiments() def boil_water(self): print(Boiling water) def pour_in_cup(self): print(Pouring into cup) abstractmethod def brew(self): pass abstractmethod def add_condiments(self): pass class Tea(CaffeineBeverage): def brew(self): print(Steeping the tea) def add_condiments(self): print(Adding lemon) class Coffee(CaffeineBeverage): def brew(self): print(Dripping coffee through filter) def add_condiments(self): print(Adding sugar and milk) if __name__ __main__: tea Tea() tea.prepare_recipe() print() coffee Coffee() coffee.prepare_recipe()11. 访问者模式 (Visitor)定义表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。生活类比医院体检病人元素接受不同科室医生访问者的检查税务申报不同类型的收入接受不同的税务计算规则Python 实现from abc import ABC, abstractmethod from typing import List class Visitor(ABC): abstractmethod def visit_employee(self, employee: Employee): pass abstractmethod def visit_department(self, department: Department): pass class OrganizationElement(ABC): abstractmethod def accept(self, visitor: Visitor): pass class Employee(OrganizationElement): def __init__(self, name: str, salary: float): self.name name self.salary salary def accept(self, visitor: Visitor): visitor.visit_employee(self) class Department(OrganizationElement): def __init__(self, name: str): self.name name self.employees: List[Employee] [] def add_employee(self, employee: Employee): self.employees.append(employee) def accept(self, visitor: Visitor): visitor.visit_department(self) for emp in self.employees: emp.accept(visitor) class SalaryReportVisitor(Visitor): def __init__(self): self.total 0 def visit_employee(self, employee: Employee): self.total employee.salary print(f{employee.name}: ${employee.salary}) def visit_department(self, department: Department): print(f\nDepartment: {department.name}) if __name__ __main__: dept Department(Engineering) dept.add_employee(Employee(Alice, 5000)) dept.add_employee(Employee(Bob, 6000)) visitor SalaryReportVisitor() dept.accept(visitor) print(f\nTotal salary: ${visitor.total})总结对比模式核心思想使用场景责任链链式传递请求多级审批、过滤器链命令封装请求为对象撤销操作、队列请求解释器定义文法解释器简单语言解析迭代器顺序访问聚合对象遍历集合中介者封装对象交互聊天室、MVC控制器备忘录捕获和恢复状态撤销操作、游戏存档观察者一对多依赖通知事件监听、消息订阅状态状态决定行为订单状态、工作流策略封装可互换算法支付方式、排序算法模板方法定义算法骨架框架设计访问者分离操作与对象编译器、报表生成

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价