当前位置:首页>python>Python 常见的设计模型:入门到精通

Python 常见的设计模型:入门到精通

  • 2026-06-29 20:16:09
Python 常见的设计模型:入门到精通

设计模式是软件工程中经过验证的、可复用的解决方案,用于解决特定上下文中反复出现的设计问题。掌握设计模式能帮助你编写更优雅、可维护、可扩展的代码。本文将以Python为例,从基础概念到高级应用,详细讲解15种常用设计模式。每种模式均包含定义问题场景解决方案代码示例(带输出)优缺点适用场景。所有代码均可复制到本地运行,助你彻底理解设计模式的精髓。


目录

  1. 设计模式简介
  2. 创建型模式
  3. 单例模式(Singleton)
  4. 工厂方法模式(Factory Method)
  5. 抽象工厂模式(Abstract Factory)
  6. 建造者模式(Builder)
  7. 结构型模式
  8. 适配器模式(Adapter)
  9. 装饰器模式(Decorator)
  10. 代理模式(Proxy)
  11. 外观模式(Facade)
  12. 行为型模式
  13. 观察者模式(Observer)
  14. 策略模式(Strategy)
  15. 模板方法模式(Template Method)
  16. 状态模式(State)
  17. 责任链模式(Chain of Responsibility)
  18. 总结与下载说明

1. 设计模式简介

设计模式主要分为三大类:

  • 创建型模式
    :对象实例化机制,如单例、工厂等。
  • 结构型模式
    :组合类或对象以形成更大结构,如适配器、装饰器等。
  • 行为型模式
    :对象之间的职责分配和通信,如观察者、策略等。

Python作为动态语言,部分模式实现更简洁(例如装饰器内置支持),但理解其背后的思想依然至关重要。


2. 创建型模式

2.1 单例模式(Singleton)

定义:确保一个类只有一个实例,并提供全局访问点。

问题场景:配置文件管理、数据库连接池、日志记录器等对象只需一个全局实例时。

解决方案:通过重写__new__方法或使用元类控制实例创建。

代码示例

classSingleton:"""经典单例实现"""    _instance = Nonedef__new__(cls, *args, **kwargs):if cls._instance isNone:            print("创建第一个实例")            cls._instance = super().__new__(cls)else:            print("返回已存在的实例")return cls._instancedef__init__(self, value=None):# 注意:__init__每次都会执行,需要控制初始化逻辑ifnot hasattr(self, 'initialized'):            self.value = value            self.initialized = True# 使用装饰器实现单例(更Pythonic)defsingleton(cls):    instances = {}defget_instance(*args, **kwargs):if cls notin instances:            instances[cls] = cls(*args, **kwargs)return instances[cls]return get_instance@singletonclassLogger:def__init__(self, level="INFO"):        self.level = level        print(f"日志级别设置为 {self.level}")# 测试if __name__ == "__main__":    print("=== 单例模式测试 ===")    s1 = Singleton("第一次")    s2 = Singleton("第二次")    print(f"s1 is s2: {s1 is s2}")  # True    print(f"s1.value = {s1.value}, s2.value = {s2.value}")  # 第二次, 注意初始化被覆盖    print("\n--- 装饰器单例 ---")    log1 = Logger("DEBUG")    log2 = Logger("ERROR")    print(f"log1 is log2: {log1 is log2}")    print(f"log1.level = {log1.level}, log2.level = {log2.level}")

输出

===单例模式测试===创建第一个实例返回已存在的实例s1 is s2:Trues1.value=第二次,s2.value=第二次---装饰器单例---日志级别设置为DEBUGlog1 is log2:Truelog1.level=DEBUG,log2.level=DEBUG

优缺点

  • 优点:节省资源,全局唯一访问点。
  • 缺点:在多线程环境需要加锁(Python可用threading.Lock);可能隐藏依赖关系。

适用场景:配置类、连接池、线程池、注册表等。


2.2 工厂方法模式(Factory Method)

定义:定义一个创建对象的接口,但由子类决定实例化哪个类。将实例化延迟到子类。

问题场景:客户端不知道需要创建的具体对象类型,或者希望解耦对象创建与使用。

解决方案:创建抽象工厂类,具体工厂重写工厂方法。

代码示例

from abc import ABC, abstractmethod# 产品抽象classAnimal(ABC):    @abstractmethoddefspeak(self):passclassDog(Animal):defspeak(self):return"汪汪"classCat(Animal):defspeak(self):return"喵喵"# 工厂抽象classAnimalFactory(ABC):    @abstractmethoddefcreate_animal(self) -> Animal:passclassDogFactory(AnimalFactory):defcreate_animal(self) -> Animal:return Dog()classCatFactory(AnimalFactory):defcreate_animal(self) -> Animal:return Cat()# 客户端defanimal_sound(factory: AnimalFactory):    animal = factory.create_animal()    print(animal.speak())if __name__ == "__main__":    print("=== 工厂方法模式 ===")    animal_sound(DogFactory())    animal_sound(CatFactory())

输出

进阶示例:参数化工厂方法

classShape(ABC):    @abstractmethoddefdraw(self):passclassCircle(Shape):defdraw(self):return"绘制圆形"classSquare(Shape):defdraw(self):return"绘制方形"classShapeFactory:defcreate_shape(self, shape_type):if shape_type == "circle":return Circle()elif shape_type == "square":return Square()else:raise ValueError("未知形状")if __name__ == "__main__":    factory = ShapeFactory()    shapes = ["circle""square""circle"]for s in shapes:        shape = factory.create_shape(s)        print(shape.draw())

输出

优缺点

  • 优点:符合开闭原则,增加新产品只需增加对应工厂类。
  • 缺点:每增加一种产品需要增加具体工厂类,系统复杂度增加。

适用场景:框架库中提供接口让用户扩展;创建逻辑复杂且经常变化。


2.3 抽象工厂模式(Abstract Factory)

定义:创建一系列相关或相互依赖对象的接口,无需指定具体类。

问题场景:系统需要生产多种产品族(如不同风格的UI组件:按钮、复选框),且产品之间搭配使用。

解决方案:定义抽象工厂,每个具体工厂生产一整套产品。

代码示例

from abc import ABC, abstractmethod# 抽象产品 AclassButton(ABC):    @abstractmethoddefpaint(self):pass# 抽象产品 BclassCheckbox(ABC):    @abstractmethoddefcheck(self):pass# 具体产品族1:Windows风格classWinButton(Button):defpaint(self):return"Win按钮绘制"classWinCheckbox(Checkbox):defcheck(self):return"Win复选框勾选"# 具体产品族2:Mac风格classMacButton(Button):defpaint(self):return"Mac按钮绘制"classMacCheckbox(Checkbox):defcheck(self):return"Mac复选框勾选"# 抽象工厂classGUIFactory(ABC):    @abstractmethoddefcreate_button(self) -> Button:pass    @abstractmethoddefcreate_checkbox(self) -> Checkbox:pass# 具体工厂classWinFactory(GUIFactory):defcreate_button(self) -> Button:return WinButton()defcreate_checkbox(self) -> Checkbox:return WinCheckbox()classMacFactory(GUIFactory):defcreate_button(self) -> Button:return MacButton()defcreate_checkbox(self) -> Checkbox:return MacCheckbox()# 客户端defcreate_ui(factory: GUIFactory):    button = factory.create_button()    checkbox = factory.create_checkbox()    print(button.paint())    print(checkbox.check())if __name__ == "__main__":    print("=== 抽象工厂模式(Windows)===")    create_ui(WinFactory())    print("\n=== 抽象工厂模式(Mac)===")    create_ui(MacFactory())

输出

=== 抽象工厂模式(Windows)===Win按钮绘制Win复选框勾选=== 抽象工厂模式(Mac)===Mac按钮绘制Mac复选框勾选

优缺点

  • 优点:保证产品族内一致性;易于交换产品系列。
  • 缺点:增加新产品族困难(需修改抽象工厂接口);扩展产品等级较麻烦。

适用场景:界面工具包、游戏风格切换、跨平台开发。


2.4 建造者模式(Builder)

定义:将复杂对象的构建与其表示分离,使同样的构建过程可以创建不同的表示。

问题场景:创建包含多个可选部分的对象(如电脑:CPU、内存、硬盘、显卡),构造函数参数过多且混乱。

解决方案:使用Director控制构建流程,Builder负责具体部件的装配。

代码示例

from abc import ABC, abstractmethod# 产品classComputer:def__init__(self):self.cpu = Noneself.ram = Noneself.storage = Noneself.gpu = Nonedef__str__(self):return f"Computer [CPU={self.cpu}, RAM={self.ram}, Storage={self.storage}, GPU={self.gpu}]"# 抽象建造者classComputerBuilder(ABC):    @abstractmethoddefbuild_cpu(self):        pass    @abstractmethoddefbuild_ram(self):        pass    @abstractmethoddefbuild_storage(self):        pass    @abstractmethoddefbuild_gpu(self):        pass    @abstractmethoddefget_result(self) -> Computer:        pass# 具体建造者:游戏电脑classGamingComputerBuilder(ComputerBuilder):def__init__(self):self.computer = Computer()defbuild_cpu(self):self.computer.cpu = "Intel i9"defbuild_ram(self):self.computer.ram = "32GB DDR5"defbuild_storage(self):self.computer.storage = "1TB NVMe SSD"defbuild_gpu(self):self.computer.gpu = "NVIDIA RTX 4090"defget_result(self) -> Computer:returnself.computer# 具体建造者:办公电脑classOfficeComputerBuilder(ComputerBuilder):def__init__(self):self.computer = Computer()defbuild_cpu(self):self.computer.cpu = "Intel i5"defbuild_ram(self):self.computer.ram = "16GB DDR4"defbuild_storage(self):self.computer.storage = "512GB SSD"defbuild_gpu(self):self.computer.gpu = "集成显卡"defget_result(self) -> Computer:returnself.computer# 指挥者classDirector:def__init__(selfbuilder: ComputerBuilder):self.builder = builderdefconstruct_computer(self):self.builder.build_cpu()self.builder.build_ram()self.builder.build_storage()self.builder.build_gpu()returnself.builder.get_result()if __name__ == "__main__":    print("=== 建造者模式 ===")    gaming_builder = GamingComputerBuilder()    director = Director(gaming_builder)    gaming_pc = director.construct_computer()    print(f"游戏电脑配置: {gaming_pc}")    office_builder = OfficeComputerBuilder()    director.builder = office_builder    office_pc = director.construct_computer()    print(f"办公电脑配置: {office_pc}")

输出

=== 建造者模式 ===游戏电脑配置: Computer [CPU=Intel i9, RAM=32GB DDR5, Storage=1TB NVMe SSD, GPU=NVIDIA RTX 4090]办公电脑配置: Computer [CPU=Intel i5, RAM=16GB DDR4, Storage=512GB SSD, GPU=集成显卡]

优缺点

  • 优点:分步构建,精细控制;复用同一构建流程创建不同表示。
  • 缺点:增加代码复杂度,需要多个类。

适用场景:复杂对象参数众多(尤其是可选参数)、配置对象、文档生成器(如PDF/HTML生成)。


3. 结构型模式

3.1 适配器模式(Adapter)

定义:将一个类的接口转换成客户希望的另一个接口,使原本因接口不兼容的类可以一起工作。

问题场景:现有类的方法签名与目标接口不符,无法直接使用。

解决方案:创建适配器类包装旧接口,转换调用。

代码示例

# 已有类(Adaptee)classEuropeanSocket:defvoltage(self):return230defplug_type(self):return"圆形插头"# 目标接口(Target)classUSASocket:defvoltage(self):return120defplug_type(self):return"扁平插头"# 适配器:将欧式插座转为美式接口classSocketAdapter(USASocket):def__init__(self, european_socket: EuropeanSocket):        self.european_socket = european_socketdefvoltage(self):# 转换电压 230 -> 120(实际需要变压器,这里模拟)return120defplug_type(self):# 转换插头形状return"扁平插头(适配)"# 客户端只能使用USASocketdefcharge_laptop(socket: USASocket):    print(f"充电电压: {socket.voltage()}V, 插头类型: {socket.plug_type()}")if __name__ == "__main__":    print("=== 适配器模式 ===")    eu_socket = EuropeanSocket()    print(f"直接使用欧式插座: 电压{eu_socket.voltage()}V, {eu_socket.plug_type()}")# 使用适配器    adapter = SocketAdapter(eu_socket)    charge_laptop(adapter)

输出

=== 适配器模式 ===直接使用欧式插座: 电压230V, 圆形插头充电电压: 120V, 插头类型: 扁平插头(适配)

进阶:类适配器(多重继承)

classEuropeanVoltage:defget_voltage(self):return230classUSASocketInterface:defsupply_power(self):return120classAdapter(EuropeanVoltage, USASocketInterface):defsupply_power(self):# 转换逻辑return120if __name__ == "__main__":    adapter = Adapter()    print(f"适配器供电: {adapter.supply_power()}V")

优缺点

  • 优点:提高类的复用性;透明性(客户端只看到目标接口)。
  • 缺点:过多适配器使系统复杂。

适用场景:集成第三方库、遗留系统改造、不同数据格式转换。


3.2 装饰器模式(Decorator)

定义:动态地给一个对象添加额外的职责,比继承更灵活。

问题场景:需要扩展一个类的功能,但又不想通过子类爆炸式增长。

解决方案:装饰器包装原有对象,在调用前后添加行为。

代码示例

from abc import ABC, abstractmethod# 组件接口classCoffee(ABC):    @abstractmethoddefcost(self) -> float:pass    @abstractmethoddefdescription(self) -> str:pass# 具体组件classSimpleCoffee(Coffee):defcost(self) -> float:return5.0defdescription(self) -> str:return"基础咖啡"# 装饰器基类classCoffeeDecorator(Coffee):def__init__(self, coffee: Coffee):        self._coffee = coffeedefcost(self) -> float:return self._coffee.cost()defdescription(self) -> str:return self._coffee.description()# 具体装饰器classMilkDecorator(CoffeeDecorator):defcost(self) -> float:return self._coffee.cost() + 1.5defdescription(self) -> str:return self._coffee.description() + ", 加奶"classSugarDecorator(CoffeeDecorator):defcost(self) -> float:return self._coffee.cost() + 0.8defdescription(self) -> str:return self._coffee.description() + ", 加糖"classWhippedCreamDecorator(CoffeeDecorator):defcost(self) -> float:return self._coffee.cost() + 2.0defdescription(self) -> str:return self._coffee.description() + ", 奶油顶"if __name__ == "__main__":    print("=== 装饰器模式 ===")    coffee = SimpleCoffee()    print(f"{coffee.description()} 价格: {coffee.cost()}元")# 加奶    coffee_with_milk = MilkDecorator(coffee)    print(f"{coffee_with_milk.description()} 价格: {coffee_with_milk.cost()}元")# 加奶加糖    coffee_with_milk_sugar = SugarDecorator(MilkDecorator(coffee))    print(f"{coffee_with_milk_sugar.description()} 价格: {coffee_with_milk_sugar.cost()}元")# 豪华版:加奶+糖+奶油    deluxe = WhippedCreamDecorator(SugarDecorator(MilkDecorator(coffee)))    print(f"{deluxe.description()} 价格: {deluxe.cost()}元")

输出

===装饰器模式===基础咖啡价格:5.0基础咖啡,加奶价格:6.5基础咖啡,加奶,加糖价格:7.3基础咖啡,加奶,加糖,奶油顶价格:9.3

Python内置装饰器语法糖:函数装饰器也是该模式的体现。

import functoolsdeflog_call(func):    @functools.wraps(func)defwrapper(*args, **kwargs):        print(f"调用 {func.__name__} 参数: {args}{kwargs}")        result = func(*args, **kwargs)        print(f"返回: {result}")return resultreturn wrapper@log_calldefadd(a, b):return a + bprint(add(35))

优缺点

  • 优点:比继承更灵活,避免类爆炸;符合开闭原则。
  • 缺点:多层装饰导致调试困难;可能产生很多小对象。

适用场景:动态添加功能(I/O流处理、GUI组件边框、日志记录等)。


3.3 代理模式(Proxy)

定义:为其他对象提供一种代理以控制对这个对象的访问。

问题场景:需要延迟加载、访问控制、日志记录或远程访问。

解决方案:代理类与真实类实现同一接口,代理持有真实对象的引用并控制访问。

代码示例

from abc import ABC, abstractmethod# 抽象主题classImage(ABC):    @abstractmethoddefdisplay(self):pass# 真实主题:大图片加载耗时classHighResolutionImage(Image):def__init__(self, filename):        self.filename = filename        self.load_from_disk()defload_from_disk(self):        print(f"正在从磁盘加载高清图片 {self.filename} (耗时3秒)...")# 模拟耗时import time        time.sleep(0.5)  # 仅演示,实际可设为更长        print(f"加载完成")defdisplay(self):        print(f"显示图片: {self.filename}")# 代理:延迟加载classImageProxy(Image):def__init__(self, filename):        self.filename = filename        self.real_image = Nonedefdisplay(self):if self.real_image isNone:            self.real_image = HighResolutionImage(self.filename)        self.real_image.display()# 客户端if __name__ == "__main__":    print("=== 代理模式(延迟加载)===")    img1 = ImageProxy("photo1.jpg")    img2 = ImageProxy("photo2.jpg")    print("第一次显示photo1:")    img1.display()    print("第二次显示photo1:")    img1.display()    print("显示photo2:")    img2.display()

输出

=== 代理模式(延迟加载)===第一次显示photo1:正在从磁盘加载高清图片 photo1.jpg (耗时3秒)...加载完成显示图片: photo1.jpg第二次显示photo1:显示图片: photo1.jpg显示photo2:正在从磁盘加载高清图片 photo2.jpg (耗时3秒)...加载完成显示图片: photo2.jpg

其他常见代理

  • 保护代理(控制访问权限)
  • 远程代理(隐藏网络通信)
  • 虚拟代理(延迟加载)

保护代理示例

classBankAccount:defwithdraw(self, amount):        print(f"取款 {amount} 元")classAccountProxy:def__init__(self, user_role):self.role = user_roleself.account = BankAccount()defwithdraw(self, amount):ifself.role == "admin":self.account.withdraw(amount)else:            print("权限不足,无法取款")if __name__ == "__main__":    proxy = AccountProxy("guest")    proxy.withdraw(100)    admin_proxy = AccountProxy("admin")    admin_proxy.withdraw(500)

输出

优缺点

  • 优点:职责清晰,可控制访问,支持延迟加载。
  • 缺点:增加系统复杂度,可能降低响应速度。

适用场景:远程代理(RPC)、虚拟代理(大对象加载)、安全代理(权限校验)。


3.4 外观模式(Facade)

定义:为子系统中的一组接口提供一个统一的简化接口。外观模式定义了一个高层接口,使子系统更易使用。

问题场景:复杂子系统依赖过多,客户端需要调用多个模块才能完成一个功能。

解决方案:创建外观类封装子系统的复杂交互。

代码示例

# 子系统类:音视频解码、投影仪、灯光、功放等classDVDPlayer:defon(self):        print("DVD播放器开机")defplay(self, movie):        print(f"播放电影 {movie}")defoff(self):        print("DVD播放器关机")classProjector:defon(self):        print("投影仪开机")defwide_screen_mode(self):        print("设置为宽屏模式")defoff(self):        print("投影仪关机")classSoundSystem:defon(self):        print("音响系统开机")defset_volume(self, level):        print(f"音量设置为 {level}")defoff(self):        print("音响系统关机")classLights:defdim(self, level):        print(f"灯光调暗至 {level}%")defon(self):        print("灯光全亮")# 外观类:家庭影院classHomeTheaterFacade:def__init__(self, dvd, projector, sound, lights):self.dvd = dvdself.projector = projectorself.sound = soundself.lights = lightsdefwatch_movie(self, movie):        print("\n准备观看电影...")self.lights.dim(20)self.projector.on()self.projector.wide_screen_mode()self.sound.on()self.sound.set_volume(60)self.dvd.on()self.dvd.play(movie)        print("开始放映!\n")defend_movie(self):        print("\n关闭电影...")self.dvd.off()self.sound.off()self.projector.off()self.lights.on()        print("电影结束,灯光亮起\n")if __name__ == "__main__":    dvd = DVDPlayer()    projector = Projector()    sound = SoundSystem()    lights = Lights()    home_theater = HomeTheaterFacade(dvd, projector, sound, lights)    home_theater.watch_movie("阿凡达")    home_theater.end_movie()

输出

准备观看电影...灯光调暗至20%投影仪开机设置为宽屏模式音响系统开机音量设置为60DVD播放器开机播放电影阿凡达开始放映!关闭电影...DVD播放器关机音响系统关机投影仪关机灯光全亮电影结束,灯光亮起

优缺点

  • 优点:简化客户端调用,降低耦合度;分层结构更清晰。
  • 缺点:可能成为上帝对象,过度耦合所有子系统。

适用场景:复杂库或框架的入口(如requests库简化HTTP调用);需要为复杂子系统提供简单接口。


4. 行为型模式

4.1 观察者模式(Observer)

定义:定义对象间一对多的依赖关系,当一个对象状态改变时,所有依赖它的对象都得到通知并自动更新。

问题场景:事件驱动系统、数据更新后需要刷新多个界面(如MVC中的模型与视图)。

解决方案:被观察者(Subject)维护观察者列表,状态变化时调用观察者更新方法。

代码示例

from abc import ABC, abstractmethod# 观察者接口classObserver(ABC):    @abstractmethoddefupdate(self, temperature, humidity, pressure):        pass# 被观察者(主题)classWeatherStation:def__init__(self):self.observers = []self.temperature = 0self.humidity = 0self.pressure = 0defattach(selfobserver: Observer):if observer notinself.observers:self.observers.append(observer)defdetach(selfobserver: Observer):self.observers.remove(observer)defnotify(self):for observer inself.observers:            observer.update(self.temperature, self.humidity, self.pressure)defset_measurements(self, temp, hum, press):self.temperature = tempself.humidity = humself.pressure = pressself.notify()# 具体观察者1:手机App显示classPhoneDisplay(Observer):defupdate(self, temperature, humidity, pressure):        print(f"手机App显示: 温度={temperature}°C, 湿度={humidity}%, 气压={pressure}hPa")# 具体观察者2:大屏幕显示器classLargeScreenDisplay(Observer):defupdate(self, temperature, humidity, pressure):        print(f"大屏幕: 当前温度 {temperature}°C | 湿度 {humidity}%")if __name__ == "__main__":    weather_station = WeatherStation()    phone = PhoneDisplay()    large_screen = LargeScreenDisplay()    weather_station.attach(phone)    weather_station.attach(large_screen)    print("=== 天气数据更新 ===")    weather_station.set_measurements(25651013)    weather_station.set_measurements(27701010)    print("\n解绑手机App后:")    weather_station.detach(phone)    weather_station.set_measurements(26681012)

输出

=== 天气数据更新 ===手机App显示: 温度=25°C, 湿度=65%, 气压=1013hPa大屏幕: 当前温度 25°C | 湿度 65%手机App显示: 温度=27°C, 湿度=70%, 气压=1010hPa大屏幕: 当前温度 27°C | 湿度 70%解绑手机App后:大屏幕: 当前温度 26°C | 湿度 68%

Python内置事件机制:可以使用weakref避免内存泄漏或使用asyncio等。

优缺点

  • 优点:支持广播通信,松耦合(主题只知道观察者接口)。
  • 缺点:观察者太多或更新频繁影响性能;循环依赖可能导致死循环。

适用场景:事件处理系统、股票行情推送、GUI监听器、模型-视图-控制器(MVC)。


4.2 策略模式(Strategy)

定义:定义一系列算法,将每个算法封装起来,并使它们可以互相替换。策略模式让算法独立于使用它的客户端而变化。

问题场景:多种支付方式、排序算法切换、折扣计算等。

解决方案:定义策略接口,具体策略实现算法,上下文类组合策略。

代码示例

from abc import ABC, abstractmethod# 策略接口classPaymentStrategy(ABC):    @abstractmethoddefpay(self, amount):pass# 具体策略1:信用卡支付classCreditCardPayment(PaymentStrategy):def__init__(self, card_number, cvv):        self.card_number = card_number        self.cvv = cvvdefpay(self, amount):        print(f"使用信用卡 {self.card_number[-4:]} 支付 {amount} 元,CVV验证通过")# 具体策略2:支付宝支付classAlipayPayment(PaymentStrategy):def__init__(self, account):        self.account = accountdefpay(self, amount):        print(f"支付宝账户 {self.account} 支付 {amount} 元")# 具体策略3:微信支付classWechatPayment(PaymentStrategy):def__init__(self, openid):        self.openid = openiddefpay(self, amount):        print(f"微信用户 {self.openid} 支付 {amount} 元")# 上下文:购物车classShoppingCart:def__init__(self):        self.items = []        self.payment_strategy = Nonedefadd_item(self, item, price):        self.items.append((item, price))deftotal(self):return sum(price for _, price in self.items)defset_payment_strategy(self, strategy: PaymentStrategy):        self.payment_strategy = strategydefcheckout(self):        total = self.total()if self.payment_strategy isNone:raise Exception("请先设置支付策略")        self.payment_strategy.pay(total)if __name__ == "__main__":    cart = ShoppingCart()    cart.add_item("Python编程书"79)    cart.add_item("机械键盘"299)    print(f"购物车总金额: {cart.total()}元")# 使用信用卡支付    cart.set_payment_strategy(CreditCardPayment("1234-5678-9012-3456""123"))    print("--- 信用卡支付 ---")    cart.checkout()# 更换策略为支付宝    cart.set_payment_strategy(AlipayPayment("alice@example.com"))    print("--- 支付宝支付 ---")    cart.checkout()

输出

购物车总金额:378---信用卡支付---使用信用卡3456支付378元,CVV验证通过---支付宝支付---支付宝账户alice@example.com支付378

结合lambda简化策略(Python特色):

strategies = {"add"lambda x, y: x + y,"subtract"lambda x, y: x - y,"multiply"lambda x, y: x * y,}defexecute_strategy(op, a, b):return strategies[op](a, b)print(execute_strategy("add"105))   # 15print(execute_strategy("multiply"34)) # 12

优缺点

  • 优点:消除条件语句,易于扩展新策略;策略可复用。
  • 缺点:客户端必须了解不同策略;增加对象数量。

适用场景:多种算法变体(排序、压缩、加密);避免长if-else或switch。


4.3 模板方法模式(Template Method)

定义:定义一个操作中的算法骨架,将某些步骤延迟到子类中实现。子类可以重定义算法的特定步骤而不改变算法结构。

问题场景:多个子类共享相同步骤,但某些步骤实现各异(如数据挖掘:读取数据、分析、输出报告)。

解决方案:抽象类定义模板方法(通常为final),其中调用基本方法(抽象或hook)。

代码示例

from abc import ABC, abstractmethod# 抽象类classDataProcessor(ABC):# 模板方法defprocess(self):        self.load_data()        self.analyze_data()        self.save_results()        self.write_report()  # hook方法可选覆写defload_data(self):        print("从文件加载数据...")    @abstractmethoddefanalyze_data(self):passdefsave_results(self):        print("保存分析结果到数据库")# hook方法(默认实现,子类可覆盖)defwrite_report(self):        print("生成标准报告")# 具体子类:销售数据分析classSalesDataProcessor(DataProcessor):defanalyze_data(self):        print("分析销售额趋势,计算同比增长率")defwrite_report(self):        print("生成销售报告PDF,附带图表")# 具体子类:用户行为分析classUserBehaviorProcessor(DataProcessor):defanalyze_data(self):        print("分析用户点击流,计算转化率")# 使用默认的write_report,不覆盖if __name__ == "__main__":    print("=== 销售数据处理 ===")    sales = SalesDataProcessor()    sales.process()    print("\n=== 用户行为数据处理 ===")    behavior = UserBehaviorProcessor()    behavior.process()

输出

=== 销售数据处理 ===从文件加载数据...分析销售额趋势,计算同比增长率保存分析结果到数据库生成销售报告PDF,附带图表=== 用户行为数据处理 ===从文件加载数据...分析用户点击流,计算转化率保存分析结果到数据库生成标准报告

进阶:钩子方法控制流程

classCoffeeMaker(ABC):defprepare_recipe(self):self.boil_water()self.brew()self.pour_in_cup()ifself.customer_wants_condiments():self.add_condiments()defboil_water(self):        print("烧开水")defpour_in_cup(self):        print("倒入杯中")    @abstractmethoddefbrew(self):        pass    @abstractmethoddefadd_condiments(self):        passdefcustomer_wants_condiments(self):return True  # 钩子classTea(CoffeeMaker):defbrew(self):        print("浸泡茶叶")defadd_condiments(self):        print("加柠檬")defcustomer_wants_condiments(self):# 询问用户        answer = input("要加柠檬吗?(y/n): ")return answer.lower() == 'y'Tea().prepare_recipe()

优缺点

  • 优点:复用代码,避免重复;符合好莱坞原则(不要调用我们,我们调用你)。
  • 缺点:每个不同实现需要子类,增加了复杂度;子类对模板方法的影响有限。

适用场景:框架基类、算法骨架固定且部分可变、工作流引擎。


4.4 状态模式(State)

定义:允许对象在其内部状态改变时改变它的行为,看起来好像修改了其类。

问题场景:对象的行为依赖于其状态,并且状态转换逻辑复杂(如电梯、订单状态机)。

解决方案:将状态封装为独立类,上下文委托给当前状态对象执行行为。

代码示例

from abc import ABC, abstractmethod# 状态接口classState(ABC):    @abstractmethoddefhandle(self, context):pass# 具体状态:已订货classOrderedState(State):defhandle(self, context):        print("订单已创建,等待支付")        context.state = PaidState()  # 状态转移# 具体状态:已支付classPaidState(State):defhandle(self, context):        print("支付完成,正在备货")        context.state = ShippedState()# 具体状态:已发货classShippedState(State):defhandle(self, context):        print("已发货,等待确认收货")        context.state = DeliveredState()# 具体状态:已完成classDeliveredState(State):defhandle(self, context):        print("订单已完成,感谢购买!")# 上下文:订单classOrder:def__init__(self):        self.state = OrderedState()defnext_state(self):        self.state.handle(self)defcancel(self):# 可定义取消逻辑,某些状态允许取消        print("订单已取消")if __name__ == "__main__":    order = Order()    order.next_state()  # 已订货 -> 等待支付    order.next_state()  # 已支付 -> 备货    order.next_state()  # 已发货 -> 等待收货    order.next_state()  # 已完成    order.next_state()  # 已完成状态无转移,将再次执行(但DeliveredState内部没有转移,只会打印)

输出

订单已创建,等待支付支付完成,正在备货已发货,等待确认收货订单已完成,感谢购买!订单已完成,感谢购买!

优化:在状态类中增加条件防止无限循环(实际开发中可以增加状态转换条件检查)。

电梯状态示例

classElevatorState(ABC):    @abstractmethoddefopen_door(self, elevator): pass    @abstractmethoddefclose_door(self, elevator): pass    @abstractmethoddefmove(self, elevator): passclassIdleState(ElevatorState):defopen_door(self, elevator):        print("开门")        elevator.state = DoorOpenState()defclose_door(self, elevator):        print("门已关,无法重复关闭")defmove(self, elevator):        print("空闲状态,未移动")classDoorOpenState(ElevatorState):defopen_door(self, elevator):        print("门已开")defclose_door(self, elevator):        print("关门")        elevator.state = IdleState()defmove(self, elevator):        print("门未关,不能移动")classElevator:def__init__(self):self.state = IdleState()defopen(self)self.state.open_door(self)defclose(self)self.state.close_door(self)defmove(self)self.state.move(self)e = Elevator()e.open()e.move()e.close()e.move()

优缺点

  • 优点:将状态转换逻辑局部化;增加新状态容易;减少条件分支。
  • 缺点:状态过多会增加类的数量;状态模式对开闭原则支持较好(增加状态不变现有代码)。

适用场景:有限状态机、游戏角色不同形态、订单/工作流状态管理。


4.5 责任链模式(Chain of Responsibility)

定义:使多个对象都有机会处理请求,避免请求发送者与接收者耦合。将这些对象连成一条链,沿着链传递请求直到被处理。

问题场景:审批流程(员工请假->主管->经理->HR)、日志级别处理、过滤器链。

解决方案:每个处理器持有下一个引用,处理不了就转发。

代码示例

from abc import ABC, abstractmethod# 处理器抽象classHandler(ABC):def__init__(self):        self._next_handler = Nonedefset_next(self, handler):        self._next_handler = handlerreturn handler    @abstractmethoddefhandle(self, request):if self._next_handler:return self._next_handler.handle(request)returnNone# 具体处理器1:技术负责人(可处理500元以下报销)classTechLead(Handler):defhandle(self, request):if request["type"] == "reimbursement"and request["amount"] <= 500:            print(f"技术负责人审批了 {request['amount']} 元报销单")returnTrueelse:            print("技术负责人无法处理,转交上级")return super().handle(request)# 具体处理器2:项目经理(可处理1000元以下)classProjectManager(Handler):defhandle(self, request):if request["type"] == "reimbursement"and request["amount"] <= 1000:            print(f"项目经理审批了 {request['amount']} 元报销单")returnTrueelse:            print("项目经理无法处理,转交上级")return super().handle(request)# 具体处理器3:总监(可处理5000元以下)classDirector(Handler):defhandle(self, request):if request["type"] == "reimbursement"and request["amount"] <= 5000:            print(f"总监审批了 {request['amount']} 元报销单")returnTrueelse:            print("总监无法处理,需要CEO批准")return super().handle(request)if __name__ == "__main__":# 构建责任链    tech_lead = TechLead()    pm = ProjectManager()    director = Director()    tech_lead.set_next(pm).set_next(director)    requests = [        {"type""reimbursement""amount"200},        {"type""reimbursement""amount"800},        {"type""reimbursement""amount"3000},        {"type""reimbursement""amount"10000},    ]for req in requests:        print(f"\n处理报销 {req['amount']} 元")        result = tech_lead.handle(req)ifnot result:            print(f"{req['amount']} 元报销未被任何领导审批")

输出

处理报销200技术负责人审批了200元报销单处理报销800技术负责人无法处理,转交上级项目经理审批了800元报销单处理报销3000技术负责人无法处理,转交上级项目经理无法处理,转交上级总监审批了3000元报销单处理报销10000技术负责人无法处理,转交上级项目经理无法处理,转交上级总监无法处理,需要CEO批准10000元报销未被任何领导审批

优缺点

  • 优点:降低耦合,增加灵活性;动态调整链。
  • 缺点:请求可能未被处理;调试困难(链路过长)。

适用场景:日志框架(不同级别去不同输出)、Servlet过滤器、事件冒泡。


5. 总结与下载说明

本文详细介绍了15种常见的设计模式,每种模式均提供了完整的Python代码示例实际运行输出,确保你可以复制代码并亲自运行验证。理解这些模式不仅能提升代码质量,还能让你在团队协作中使用统一的术语交流。

额外建议

  • 学习模式时,先理解问题场景,不要为了用模式而用模式。
  • 结合Python内置特性(如装饰器、上下文管理器)与模式思想,写出更Pythonic的代码。
  • 每个模式都试着自己重写一遍,加深印象。

希望这篇长文能成为你Python设计模式之路的得力助手。如果有任何疑问或建议,欢迎在评论区交流!

Happy Coding!

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-07-04 08:11:40 HTTP/2.0 GET : https://f.mffb.com.cn/a/491748.html
  2. 运行时间 : 0.871223s [ 吞吐率:1.15req/s ] 内存消耗:5,053.42kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=658e712ba5042c173df2468c903a9ff5
  1. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/public/index.php ( 0.79 KB )
  2. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/autoload.php ( 0.17 KB )
  3. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/provider.php ( 0.19 KB )
  23. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/common.php ( 0.03 KB )
  27. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/app.php ( 0.95 KB )
  30. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cache.php ( 0.78 KB )
  31. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/console.php ( 0.23 KB )
  32. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/cookie.php ( 0.56 KB )
  33. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/database.php ( 2.48 KB )
  34. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/filesystem.php ( 0.61 KB )
  36. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/lang.php ( 0.91 KB )
  37. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/log.php ( 1.35 KB )
  38. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/middleware.php ( 0.19 KB )
  39. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/route.php ( 1.89 KB )
  40. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/session.php ( 0.57 KB )
  41. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/trace.php ( 0.34 KB )
  42. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/config/view.php ( 0.82 KB )
  43. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/event.php ( 0.25 KB )
  44. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/service.php ( 0.13 KB )
  46. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/AppService.php ( 0.26 KB )
  47. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/services.php ( 0.14 KB )
  53. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/Request.php ( 0.09 KB )
  84. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/middleware.php ( 0.25 KB )
  86. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/route/app.php ( 1.72 KB )
  100. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/controller/Index.php ( 4.81 KB )
  104. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/app/BaseController.php ( 2.05 KB )
  105. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/runtime/temp/067d451b9a0c665040f3f1bdd3293d68.php ( 11.98 KB )
  140. /yingpanguazai/ssd/ssd1/www/f.mffb.com.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.001181s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.001395s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.101317s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.067087s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.000883s ]
  6. SELECT * FROM `set` [ RunTime:0.011107s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.000705s ]
  8. SELECT * FROM `article` WHERE `id` = 491748 LIMIT 1 [ RunTime:0.100941s ]
  9. UPDATE `article` SET `lasttime` = 1783123900 WHERE `id` = 491748 [ RunTime:0.117412s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.009038s ]
  11. SELECT * FROM `article` WHERE `id` < 491748 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.010749s ]
  12. SELECT * FROM `article` WHERE `id` > 491748 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.017768s ]
  13. SELECT * FROM `article` WHERE `id` < 491748 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.063390s ]
  14. SELECT * FROM `article` WHERE `id` < 491748 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.185389s ]
  15. SELECT * FROM `article` WHERE `id` < 491748 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.032115s ]
0.873755s