当前位置:首页>python>Python设计模式应用最佳实践总结

Python设计模式应用最佳实践总结

  • 2026-04-27 20:56:19
Python设计模式应用最佳实践总结

嘿,Python学习搭子!在掌握了设计模式的基本概念和实现之后,咱们今天要来聊聊最重要的一课——如何在实际项目中正确应用设计模式。就像学会了烹饪技巧,还要知道什么时候用大火、什么时候用小火一样,设计模式的应用也需要遵循一些最佳实践。

🎯 本文学习目标

通过这篇最佳实践指南,你将学会:

  1. 1. 明智选择模式:根据场景选择最合适的模式,避免"为了模式而模式"
  2. 2. Pythonic实现:用Python特有的方式优雅实现设计模式
  3. 3. 规避常见陷阱:识别并避免设计模式应用中的典型错误
  4. 4. 编写可测试代码:让设计模式友好的代码更容易测试
  5. 5. 团队协作规范:建立团队内的设计模式使用约定

准备好了吗?咱们这就开始实践之旅!

第一部分:设计模式的选择原则

1.1 设计模式不是银弹

首先,咱们要明确一个核心原则:设计模式是解决问题的工具,不是目标本身

反例:过度设计

# 反例:简单需求过度使用设计模式
classUser:
def__init__(self, name, email):
self.name = name
self.email = email

# 不必要的工厂模式
classUserFactory:
    @staticmethod
defcreate(name, email):
return User(name, email)

# 不必要的仓储模式
classUserRepository:
def__init__(self):
self.users = {}

defsave(self, user):
self.users[user.name] = user

# 实际使用
user = UserFactory.create("张三""zhangsan@example.com")
repo = UserRepository()
repo.save(user)

正例:保持简单

# 正例:直接使用简单结构
user = {"name""张三""email""zhangsan@example.com"}
users = {}  # 简单的字典存储
users[user["name"]] = user

# 或者使用数据类
from dataclasses import dataclass

@dataclass
classUser:
    name: str
    email: str

user = User("张三""zhangsan@example.com")

1.2 设计模式选择决策树

当遇到问题时,可以按照以下流程判断是否需要使用设计模式:

1. 问题是否复杂?
   ├── 否 → 直接实现,保持简单
   └── 是 → 继续

2. 是否有明确的设计模式适用?
   ├── 否 → 考虑其他架构方案
   └── 是 → 继续

3. 引入模式的收益 > 复杂度成本?
   ├── 否 → 简化方案
   └── 是 → 使用该模式

1.3 不同场景的推荐模式

场景
推荐模式
理由
Python特性利用
对象创建逻辑复杂
工厂模式、建造者模式
封装复杂创建逻辑
类方法、@classmethod
需要动态扩展功能
装饰器模式、策略模式
运行时灵活组合
函数装饰器、一等函数
对象间依赖通知
观察者模式、发布-订阅
松耦合通信
asyncio、回调
接口不兼容
适配器模式、外观模式
整合遗留代码
__getattr__
、猴子补丁
算法族选择
策略模式、模板方法
避免条件分支
函数作为参数
状态管理复杂
状态模式
简化状态转换
枚举、字典映射

1.4 设计模式组合原则

在实际项目中,设计模式很少单独使用。合理的组合能发挥更大威力:

# 组合示例:工厂 + 策略 + 装饰器
classPaymentMethod:
"""支付策略接口"""
defpay(self, amount: float) -> str:
pass

classAlipay(PaymentMethod):
defpay(self, amount: float) -> str:
returnf"支付宝支付 {amount}"

classWechatPay(PaymentMethod):
defpay(self, amount: float) -> str:
returnf"微信支付 {amount}"

# 装饰器:添加日志
deflog_payment(func):
defwrapper(self, amount):
print(f"[LOG] 开始支付: {amount}")
        result = func(self, amount)
print(f"[LOG] 支付完成: {result}")
return result
return wrapper

classLoggedWechatPay(WechatPay):
    @log_payment
defpay(self, amount: float) -> str:
returnsuper().pay(amount)

# 工厂:创建支付实例
classPaymentFactory:
    @staticmethod
defcreate(method: str) -> PaymentMethod:
if method == "alipay":
return Alipay()
elif method == "wechat":
return LoggedWechatPay()  # 返回带日志的版本
else:
raise ValueError(f"不支持的支付方式: {method}")

# 使用
payment = PaymentFactory.create("wechat")
result = payment.pay(100.0)

第二部分:Pythonic实现最佳实践

2.1 利用Python语言特性

Python的动态特性让很多设计模式的实现变得极其简洁:

2.1.1 一等函数替代策略模式

# 传统策略模式(面向对象)
classDiscountStrategy:
defcalculate(self, price: float) -> float:
pass

classNoDiscount(DiscountStrategy):
defcalculate(self, price: float) -> float:
return price

classPercentageDiscount(DiscountStrategy):
def__init__(self, percentage: float):
self.percentage = percentage

defcalculate(self, price: float) -> float:
return price * (1 - self.percentage / 100)

# Pythonic策略模式(使用函数)
defno_discount(price: float) -> float:
return price

defpercentage_discount(percentage: float):
defdiscount_func(price: float) -> float:
return price * (1 - percentage / 100)
return discount_func

# 使用字典映射策略
discount_strategies = {
"none": no_discount,
"10%": percentage_discount(10),
"20%": percentage_discount(20)
}

defcalculate_price(price: float, strategy_name: str) -> float:
    strategy = discount_strategies.get(strategy_name, no_discount)
return strategy(price)

# 测试
print(calculate_price(100.0"10%"))  # 90.0
print(calculate_price(100.0"none"))  # 100.0

2.1.2 装饰器语法糖

# 传统装饰器模式
classComponent:
defoperation(self) -> str:
return"原始组件"

classDecorator(Component):
def__init__(self, component: Component):
self._component = component

defoperation(self) -> str:
returnself._component.operation()

classConcreteDecoratorA(Decorator):
defoperation(self) -> str:
returnf"装饰器A({super().operation()})"

# 使用
component = Component()
decorated = ConcreteDecoratorA(component)
print(decorated.operation())  # 装饰器A(原始组件)

# Pythonic装饰器模式
defcomponent() -> str:
return"原始组件"

defdecorator_a(func):
defwrapper():
returnf"装饰器A({func()})"
return wrapper

@decorator_a
defdecorated_component() -> str:
return component()

print(decorated_component())  # 装饰器A(原始组件)

2.1.3 上下文管理器替代复杂初始化

# 传统建造者模式
classDatabaseConnectionBuilder:
def__init__(self):
self._config = {}

defset_host(self, host: str):
self._config["host"] = host
returnself

defset_port(self, port: int):
self._config["port"] = port
returnself

defset_timeout(self, timeout: float):
self._config["timeout"] = timeout
returnself

defbuild(self):
return DatabaseConnection(**self._config)

# 使用
conn = (DatabaseConnectionBuilder()
        .set_host("localhost")
        .set_port(5432)
        .set_timeout(30.0)
        .build())

# Pythonic方式:使用上下文管理器
import contextlib
from contextlib import contextmanager

@contextmanager
defdatabase_connection(host: str = "localhost"
                       port: int = 5432
                       timeout: float = 30.0
):
"""创建数据库连接的上下文管理器"""
    conn = DatabaseConnection(host=host, port=port, timeout=timeout)
try:
        conn.connect()
yield conn
finally:
        conn.disconnect()

# 使用
with database_connection(host="db.example.com", timeout=60.0as conn:
    results = conn.execute_query("SELECT * FROM users")

2.2 数据类的正确使用

Python 3.7+的数据类(dataclasses)能极大简化值对象的创建:

from dataclasses import dataclass, field
from typing importListOptional
from datetime import datetime

# 传统方式
classTraditionalOrder:
def__init__(self, order_id: str, customer_id: str, items: List[str], 
                 total: float, created_at: datetime = None
):
self.order_id = order_id
self.customer_id = customer_id
self.items = items
self.total = total
self.created_at = created_at or datetime.now()

def__repr__(self):
returnf"Order({self.order_id}{self.customer_id}{self.total})"

def__eq__(self, other):
ifnotisinstance(other, TraditionalOrder):
returnFalse
returnself.order_id == other.order_id

# 数据类方式
@dataclass(order=True)  # 启用排序
classOrder:
    order_id: str
    customer_id: str
    items: List[str] = field(default_factory=list)
    total: float = 0.0
    created_at: datetime = field(default_factory=datetime.now)
    status: str = "created"

    @property
defitem_count(self) -> int:
returnlen(self.items)

defapply_discount(self, percentage: float):
"""应用折扣"""
        discount = self.total * percentage / 100
self.total -= discount
returnself

# 使用数据类的优势
order1 = Order("ORD001""CUST001", ["Book"], 50.0)
order2 = Order("ORD002""CUST002", ["Pen"], 10.0)

print(order1)  # 自动生成__repr__
print(order1 == order2)  # 自动生成__eq__
print(order1.item_count)  # 自定义属性

# 数据类还支持:
# - 不可变实例:@dataclass(frozen=True)
# - 后初始化:__post_init__方法
# - 字段元数据:field(metadata={"unit": "USD"})

2.3 协议(Protocol)替代抽象基类

Python 3.8+的Protocol提供了更灵活的接口定义方式:

from typing import Protocol, runtime_checkable
from dataclasses import dataclass

# 传统方式:抽象基类
from abc import ABC, abstractmethod

classNotificationService(ABC):
    @abstractmethod
defsend(self, message: str, recipient: str) -> bool:
pass

classEmailService(NotificationService):
defsend(self, message: str, recipient: str) -> bool:
print(f"发送邮件给 {recipient}{message}")
returnTrue

# 协议方式:更灵活
@runtime_checkable
classNotificationProtocol(Protocol):
defsend(self, message: str, recipient: str) -> bool:
        ...

# 任何实现了send方法的类都符合协议
classSMSService:
defsend(self, message: str, phone_number: str) -> bool:
print(f"发送短信给 {phone_number}{message}")
returnTrue

classPushNotificationService:
defsend(self, content: str, device_token: str) -> bool:
print(f"推送通知到设备 {device_token}{content}")
returnTrue

# 协议检查
defcheck_notification_service(service: NotificationProtocol):
returnisinstance(service, NotificationProtocol)

# 使用
sms = SMSService()
print(check_notification_service(sms))  # True,因为SMSService有send方法

push = PushNotificationService()
print(check_notification_service(push))  # True

# 鸭子类型的好处:不需要显式继承

2.4 生成器与协程的妙用

对于迭代器模式和状态模式,生成器和协程提供了更优雅的实现:

# 传统迭代器模式
classFibonacciIterator:
def__init__(self, max_count: int = 10):
self.max_count = max_count
self.count = 0
self.a, self.b = 01

def__iter__(self):
returnself

def__next__(self):
ifself.count >= self.max_count:
raise StopIteration

        result = self.a
self.a, self.b = self.b, self.a + self.b
self.count += 1
return result

# Pythonic方式:生成器
deffibonacci_generator(max_count: int = 10):
    a, b = 01
    count = 0
while count < max_count:
yield a
        a, b = b, a + b
        count += 1

# 使用
for num in fibonacci_generator(5):
print(num)

# 协程实现状态机
asyncdeforder_state_machine():
"""订单状态机的协程实现"""
    state = "created"

whileTrue:
        event = yield state

if state == "created"and event == "pay":
            state = "paid"
print("订单已支付")

elif state == "paid"and event == "ship":
            state = "shipped"
print("订单已发货")

elif state == "shipped"and event == "deliver":
            state = "delivered"
print("订单已送达")

else:
print(f"无效状态转换: {state} -> {event}")

# 使用
asyncdefmain():
    state_machine = order_state_machine()
await state_machine.asend(None)  # 初始化

for event in ["pay""ship""deliver""invalid"]:
        state = await state_machine.asend(event)
print(f"当前状态: {state}")

# asyncio.run(main())

第三部分:常见误区和陷阱

3.1 设计模式滥用案例

案例1:单例模式过度使用

# 错误:处处使用单例
classConfigSingleton:
    _instance = None

def__new__(cls):
if cls._instance isNone:
            cls._instance = super().__new__(cls)
return cls._instance

def__init__(self):
self.settings = {}

classLoggerSingleton:
    _instance = None

def__new__(cls):
if cls._instance isNone:
            cls._instance = super().__new__(cls)
return cls._instance

classCacheSingleton:
    _instance = None

def__new__(cls):
if cls._instance isNone:
            cls._instance = super().__new__(cls)
return cls._instance

# 正确:根据需求选择
# 1. 配置:可以使用模块级变量
config = {"debug"True"timeout"30}

# 2. 日志:使用标准库logging模块
import logging
logging.basicConfig(level=logging.INFO)

# 3. 缓存:根据需要选择
from functools import lru_cache

@lru_cache(maxsize=128)
defexpensive_computation(n):
return n * n

案例2:过度抽象和模式嵌套

# 错误:过度复杂的抽象层次
classAbstractNotification:
defsend(self, message: str):
pass

classEmailNotification(AbstractNotification):
pass

classDecoratedEmailNotification(EmailNotification):
pass

classNotificationFactory:
defcreate(self, typestr) -> AbstractNotification:
pass

classNotificationManager:
def__init__(self, factory: NotificationFactory):
self.factory = factory

defnotify(self, typestr, message: str):
        notification = self.factory.create(type)
        notification.send(message)

# 正确:简化设计
defsend_email(message: str, recipient: str):
print(f"发送邮件给 {recipient}{message}")

defsend_sms(message: str, phone: str):
print(f"发送短信给 {phone}{message}")

notification_functions = {
"email": send_email,
"sms": send_sms
}

defnotify(typestr, message: str, recipient: str):
    func = notification_functions.get(type)
if func:
        func(message, recipient)

3.2 性能陷阱

陷阱1:装饰器链过长

import time
from functools import wraps

# 多个装饰器嵌套
deftiming_decorator(func):
    @wraps(func)
defwrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
print(f"{func.__name__} 耗时: {time.time() - start:.4f}秒")
return result
return wrapper

deflogging_decorator(func):
    @wraps(func)
defwrapper(*args, **kwargs):
print(f"调用 {func.__name__}")
return func(*args, **kwargs)
return wrapper

defvalidation_decorator(func):
    @wraps(func)
defwrapper(*args, **kwargs):
ifnot args:
raise ValueError("参数不能为空")
return func(*args, **kwargs)
return wrapper

# 三层装饰器
@timing_decorator
@logging_decorator
@validation_decorator
defcomplex_calculation(n):
returnsum(range(n))

# 每次调用都有三层函数调用开销
# 解决方案:合并装饰器或使用类装饰器

陷阱2:不必要的对象创建

# 错误:频繁创建策略对象
defprocess_data(data, strategy_name):
if strategy_name == "strategy_a":
        strategy = StrategyA()
elif strategy_name == "strategy_b":
        strategy = StrategyB()
else:
        strategy = DefaultStrategy()

return strategy.process(data)

# 频繁调用时,每次都要创建新对象
for i inrange(10000):
    process_data(data, "strategy_a")

# 正确:复用对象
classStrategyFactory:
def__init__(self):
self._strategies = {
"strategy_a": StrategyA(),
"strategy_b": StrategyB(),
"default": DefaultStrategy()
        }

defget_strategy(self, name: str):
returnself._strategies.get(name, self._strategies["default"])

factory = StrategyFactory()

defoptimized_process_data(data, strategy_name):
    strategy = factory.get_strategy(strategy_name)
return strategy.process(data)

3.3 可维护性陷阱

陷阱1:缺乏文档的模式应用

# 错误:没有文档的复杂模式
classA:
def__init__(self, b):
self.b = b
self.c = C()
self.d = D(self.b, self.c)
self.e = E(self.d)

# 正确:添加文档和类型提示
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from .b_module import B
from .c_module import C
from .d_module import D
from .e_module import E

classServiceCoordinator:
"""
    服务协调器:组合多个服务完成复杂业务

    设计模式:
    1. 外观模式:对外提供统一接口
    2. 依赖注入:通过构造函数注入依赖
    3. 组合模式:组合多个服务

    依赖关系:
    ServiceCoordinator → B → C
                     ↓
                     D → E
    """


def__init__(self, b_service: 'B', c_service: 'C'):
self.b = b_service
self.c = c_service
self.d = D(b_service, c_service)
self.e = E(self.d)

陷阱2:模式导致的过度耦合

# 错误:观察者模式导致的隐式耦合
classOrderService:
def__init__(self):
self._observers = []  # 内部维护观察者

defcreate_order(self, data):
        order = self._save_order(data)

# 隐式调用多个服务
for observer inself._observers:
            observer.on_order_created(order)

return order

# 正确:显式依赖,更易理解
classOrderService:
def__init__(self, 
                 inventory_service: InventoryService,
                 payment_service: PaymentService,
                 notification_service: NotificationService
):
self.inventory = inventory_service
self.payment = payment_service
self.notification = notification_service

defcreate_order(self, data):
        order = self._save_order(data)

# 显式调用
self.inventory.reserve_stock(order)
self.payment.process_payment(order)
self.notification.send_confirmation(order)

return order

第四部分:测试策略与可维护性

4.1 设计模式友好的测试

4.1.1 依赖注入与测试替身

from typing import Protocol
import unittest.mock as mock

# 定义协议
classPaymentGateway(Protocol):
defcharge(self, amount: float, token: str) -> dict:
        ...

# 业务类使用依赖注入
classOrderProcessor:
def__init__(self, payment_gateway: PaymentGateway):
self.payment = payment_gateway

defprocess_order(self, order_data: dict) -> bool:
# 使用注入的依赖
        result = self.payment.charge(
            order_data["amount"],
            order_data["payment_token"]
        )
return result.get("success"False)

# 测试:使用Mock对象
deftest_order_processor():
# 创建Mock支付网关
    mock_gateway = mock.Mock(spec=PaymentGateway)
    mock_gateway.charge.return_value = {"success"True}

# 注入Mock
    processor = OrderProcessor(mock_gateway)

# 测试数据
    order_data = {
"amount"100.0,
"payment_token""tok_123"
    }

# 执行测试
    result = processor.process_order(order_data)

# 验证
assert result isTrue
    mock_gateway.charge.assert_called_once_with(100.0"tok_123")

4.1.2 工厂模式的测试

from abc import ABC, abstractmethod
import pytest

# 抽象产品
classFormatter(ABC):
    @abstractmethod
defformat(self, data: str) -> str:
pass

# 具体产品
classJSONFormatter(Formatter):
defformat(self, data: str) -> str:
import json
return json.dumps({"data": data})

classXMLFormatter(Formatter):
defformat(self, data: str) -> str:
returnf"<data>{data}</data>"

# 工厂
classFormatterFactory:
    @staticmethod
defcreate(format_type: str) -> Formatter:
if format_type == "json":
return JSONFormatter()
elif format_type == "xml":
return XMLFormatter()
else:
raise ValueError(f"不支持的格式: {format_type}")

# 测试工厂
deftest_formatter_factory():
# 测试JSON格式化器
    json_formatter = FormatterFactory.create("json")
    result = json_formatter.format("test")
assert result == '{"data": "test"}'

# 测试XML格式化器
    xml_formatter = FormatterFactory.create("xml")
    result = xml_formatter.format("test")
assert result == "<data>test</data>"

# 测试无效类型
with pytest.raises(ValueError):
        FormatterFactory.create("invalid")

# 使用pytest的参数化测试
@pytest.mark.parametrize("format_type, input_data, expected", [
    ("json""hello"'{"data": "hello"}'),
    ("xml""world""<data>world</data>"),
]
)

deftest_formatters(format_type, input_data, expected):
    formatter = FormatterFactory.create(format_type)
    result = formatter.format(input_data)
assert result == expected

4.2 可测试的设计模式实现

4.2.1 可测试的观察者模式

import asyncio
from typing importSetCallableAny
import pytest

classObservable:
"""可测试的观察者模式实现"""

def__init__(self):
self._observers: Set[Callable] = set()

defsubscribe(self, observer: Callable):
self._observers.add(observer)

defunsubscribe(self, observer: Callable):
self._observers.discard(observer)

defnotify(self, event_data: Any):
for observer inself._observers:
            observer(event_data)

    @property
defobserver_count(self) -> int:
returnlen(self._observers)

# 测试用例
classTestObservable:
defsetup_method(self):
self.observable = Observable()

deftest_subscribe(self):
defobserver(data):
pass

self.observable.subscribe(observer)
assertself.observable.observer_count == 1

deftest_unsubscribe(self):
defobserver(data):
pass

self.observable.subscribe(observer)
self.observable.unsubscribe(observer)
assertself.observable.observer_count == 0

deftest_notify(self):
        received_data = []

defobserver(data):
            received_data.append(data)

self.observable.subscribe(observer)
self.observable.notify("test_event")

assert received_data == ["test_event"]

deftest_multiple_observers(self):
        results = []

defobserver1(data):
            results.append(f"observer1: {data}")

defobserver2(data):
            results.append(f"observer2: {data}")

self.observable.subscribe(observer1)
self.observable.subscribe(observer2)
self.observable.notify("event")

assertlen(results) == 2
assert"observer1: event"in results
assert"observer2: event"in results

4.2.2 策略模式的测试

from typing importListCallable
import pytest

# 策略接口
SortingStrategy = Callable[[List[int]], List[int]]

# 具体策略
defbubble_sort(numbers: List[int]) -> List[int]:
"""冒泡排序"""
    n = len(numbers)
for i inrange(n):
for j inrange(0, n - i - 1):
if numbers[j] > numbers[j + 1]:
                numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
return numbers

defquick_sort(numbers: List[int]) -> List[int]:
"""快速排序"""
iflen(numbers) <= 1:
return numbers

    pivot = numbers[len(numbers) // 2]
    left = [x for x in numbers if x < pivot]
    middle = [x for x in numbers if x == pivot]
    right = [x for x in numbers if x > pivot]

return quick_sort(left) + middle + quick_sort(right)

# 上下文
classSorter:
def__init__(self, strategy: SortingStrategy):
self.strategy = strategy

defsort(self, numbers: List[int]) -> List[int]:
returnself.strategy(numbers.copy())  # 避免修改原数据

defset_strategy(self, strategy: SortingStrategy):
self.strategy = strategy

# 测试
classTestSorter:
    @pytest.fixture
defunsorted_list(self):
return [64342512221190]

    @pytest.fixture
defsorted_list(self):
return [11122225346490]

deftest_bubble_sort(self, unsorted_list, sorted_list):
        sorter = Sorter(bubble_sort)
        result = sorter.sort(unsorted_list)
assert result == sorted_list

deftest_quick_sort(self, unsorted_list, sorted_list):
        sorter = Sorter(quick_sort)
        result = sorter.sort(unsorted_list)
assert result == sorted_list

deftest_strategy_switching(self, unsorted_list, sorted_list):
        sorter = Sorter(bubble_sort)
        result1 = sorter.sort(unsorted_list)

        sorter.set_strategy(quick_sort)
        result2 = sorter.sort(unsorted_list)

assert result1 == sorted_list
assert result2 == sorted_list

4.3 持续集成与设计模式

4.3.1 测试覆盖率保障

# .coveragerc 配置文件
[run]
source = .
omit =
    */tests/*
    */migrations/*
    */__pycache__/*
    */venv/*
    */.venv/*

[report]
exclude_lines =
    pragma: no cover
def__repr__
ifself.debug
if settings.DEBUG
raise AssertionError
raise NotImplementedError
if0:
if __name__ == .__main__.:
class .*\bProtocol\b:
    @.*abc.abstractmethod

# pytest配置文件:pyproject.toml
[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
addopts = [
"--verbose",
"--tb=short",
"--strict-markers",
"--cov=.",
"--cov-report=term-missing",
"--cov-report=html"
]
markers = [
"unit: 单元测试",
"integration: 集成测试",
"slow: 慢速测试",
"design_pattern: 设计模式相关测试"
]

4.3.2 设计模式测试套件

# tests/test_design_patterns.py
import pytest
from design_patterns.factory import PaymentFactory
from design_patterns.strategy import Sorter, bubble_sort, quick_sort
from design_patterns.observer import Observable

classTestDesignPatterns:
"""设计模式测试套件"""

    @pytest.mark.design_pattern
    @pytest.mark.factory
deftest_payment_factory(self):
"""测试工厂模式"""
        alipay = PaymentFactory.create("alipay")
assert alipay.process(100isTrue

        wechat = PaymentFactory.create("wechat")
assert wechat.process(200isTrue

with pytest.raises(ValueError):
            PaymentFactory.create("invalid")

    @pytest.mark.design_pattern
    @pytest.mark.strategy
deftest_sorting_strategies(self):
"""测试策略模式"""
        sorter = Sorter(bubble_sort)
        result = sorter.sort([31415])
assert result == [11345]

        sorter.set_strategy(quick_sort)
        result = sorter.sort([31415])
assert result == [11345]

    @pytest.mark.design_pattern
    @pytest.mark.observer
deftest_observer_pattern(self):
"""测试观察者模式"""
        observable = Observable()
        events = []

defobserver(data):
            events.append(data)

        observable.subscribe(observer)
        observable.notify("event1")

assert events == ["event1"]

        observable.unsubscribe(observer)
        observable.notify("event2")

assert events == ["event1"]  # 取消订阅后不应收到事件

# 运行特定标记的测试
# pytest -m "design_pattern and factory" -v
# pytest -m "design_pattern and strategy" -v

第五部分:团队协作与代码规范

5.1 设计模式命名约定

5.1.1 类命名约定

# 工厂模式
classPaymentFactory:          # 主工厂类
classPaymentCreator:          # 替代命名
classPaymentBuilder:          # 建造者模式

# 策略模式
classSortingStrategy:         # 策略接口
classQuickSortStrategy:       # 具体策略
classBubbleSortStrategy:      # 具体策略

# 观察者模式
classOrderSubject:            # 被观察者
classOrderObserver:           # 观察者接口
classEmailObserver:           # 具体观察者

# 装饰器模式
classLoggerDecorator:         # 装饰器类
classTimedOperation:          # 功能描述

# 适配器模式
classLegacySystemAdapter:     # 适配器类
classNewInterfaceAdapter:     # 适配器类

5.1.2 方法命名约定

classNotificationService:
# 观察者模式方法
defsubscribe(self, observer):     # 订阅
defunsubscribe(self, observer):   # 取消订阅
defnotify(self, event):           # 通知

# 工厂模式方法
    @classmethod
defcreate(cls, type):             # 创建实例
    @classmethod
deffrom_config(cls, config):      # 从配置创建

# 策略模式方法
defset_strategy(self, strategy):  # 设置策略
defexecute(self, data):           # 执行策略

5.2 代码审查清单

5.2.1 设计模式使用审查点

# 设计模式代码审查清单

## 1. 模式选择合理性
- [ ] 是否真的需要设计模式?
- [ ] 所选模式是否最适合当前场景?
- [ ] 是否有更简单的解决方案?

## 2. 实现正确性
- [ ] 模式的核心概念是否正确实现?
- [ ] 是否符合Pythonic原则?
- [ ] 是否有不必要的复杂性?

## 3. 可测试性
- [ ] 代码是否易于单元测试?
- [ ] 依赖是否清晰可注入?
- [ ] Mock对象是否容易创建?

## 4. 可维护性
- [ ] 命名是否清晰表达意图?
- [ ] 文档是否充分说明设计模式的使用?
- [ ] 是否有过度抽象或模式嵌套?

## 5. 性能考量
- [ ] 模式引入的开销是否可以接受?
- [ ] 是否有不必要的对象创建?
- [ ] 是否有性能热点?

5.2.2 设计模式反模式识别

# 反模式识别代码示例
defdetect_anti_patterns(code_snippet: str) -> List[str]:
"""检测设计模式反模式"""
    anti_patterns = []

# 1. 过度使用单例模式
if re.search(r"class .*Singleton.*:", code_snippet) and \
       re.search(r"_instance", code_snippet):
        anti_patterns.append("过度使用单例模式")

# 2. 不必要的抽象工厂
if re.search(r"Abstract.*Factory", code_snippet) and \
       re.search(r"create.*product.*family", code_snippet, re.IGNORECASE):
# 检查是否真的需要产品族
        anti_patterns.append("可能不必要的抽象工厂")

# 3. 过深的装饰器链
    decorator_count = code_snippet.count("@")
if decorator_count > 3:
        anti_patterns.append(f"过深的装饰器链({decorator_count}层)")

return anti_patterns

5.3 团队知识共享

5.3.1 设计模式决策文档

# 设计模式决策记录 (ADR)

## ADR-001: 订单系统中使用策略模式

### 状态
已采纳

### 背景
订单支付支持多种方式(支付宝、微信、信用卡),
每次新增支付方式需要修改核心业务逻辑。

### 决策
采用策略模式封装支付逻辑。

### 理由
1. 符合开闭原则:新增支付方式不修改现有代码
2. 提高可测试性:每种支付策略可独立测试
3. 符合团队技术栈:Python函数作为策略实现简单

### 后果
**正面:**
- 支付逻辑解耦
- 代码更易维护
- 新成员容易理解

**负面:**
- 增加了少量抽象层
- 需要团队成员理解策略模式

### 实施示例
```python
def alipay_pay(amount):
    pass

def wechat_pay(amount):
    pass

payment_strategies = {
    'alipay': alipay_pay,
    'wechat': wechat_pay
}
#### 5.3.2 设计模式学习资源

```python
# design_patterns/__init__.py
"""
Python设计模式最佳实践库

包含内容:
1. 常用设计模式的Pythonic实现
2. 测试示例和模式
3. 代码审查清单
4. 性能优化建议

使用指南:
1. 先阅读相应模式的理论说明
2. 查看实现示例
3. 参考测试用例
4. 应用代码审查清单

贡献指南:
1. 新增模式前确认是否已有类似实现
2. 提供充分的测试用例
3. 包含性能基准测试(如需要)
4. 更新相关文档
"""

5.4 渐进式重构策略

5.4.1 从简单到复杂的演进

# 阶段1:简单实现
defprocess_order(order_data):
# 直接处理
    save_to_db(order_data)
    send_email(order_data["email"], "订单确认")
    update_inventory(order_data["items"])
returnTrue

# 阶段2:函数分离
defprocess_order(order_data):
    save_order(order_data)
    notify_customer(order_data)
    update_stock(order_data)
returnTrue

defsave_order(order_data):
pass

defnotify_customer(order_data):
pass

defupdate_stock(order_data):
pass

# 阶段3:引入策略模式(如果需要)
classOrderProcessor:
def__init__(self, notification_strategy, inventory_strategy):
self.notify = notification_strategy
self.update_stock = inventory_strategy

defprocess(self, order_data):
self.save(order_data)
self.notify(order_data)
self.update_stock(order_data)
returnTrue

defsave(self, order_data):
pass

# 阶段4:引入观察者模式(如果需要)
classOrderEventSystem:
def__init__(self):
self._observers = []

defprocess_order(self, order_data):
self.save(order_data)
self._notify("order.created", order_data)
returnTrue

def_notify(self, event_type, data):
for observer inself._observers:
            observer(event_type, data)

5.4.2 重构检查清单

classRefactoringChecklist:
"""重构设计模式检查清单"""

    @staticmethod
defbefore_refactoring(code_snippet):
"""重构前检查"""
        checks = {
"test_coverage""重构前是否有足够的测试覆盖率?",
"backup""代码是否已备份或提交到版本控制?",
"understanding""是否充分理解现有代码逻辑?",
"impact""是否评估了重构对系统其他部分的影响?"
        }
return checks

    @staticmethod
defduring_refactoring():
"""重构过程中"""
        principles = [
"小步前进,频繁测试",
"保持功能不变",
"每次提交一个小的变化",
"使用版本控制分支"
        ]
return principles

    @staticmethod
defafter_refactoring():
"""重构后验证"""
        validations = [
"所有测试用例通过",
"性能没有明显下降",
"代码审查通过",
"文档已更新"
        ]
return validations

第六部分:总结与持续改进

6.1 关键原则回顾

通过这篇最佳实践指南,咱们一起总结了设计模式应用的核心理念:

  1. 1. 合适性原则:选择最适合当前场景的模式,而不是最复杂的
  2. 2. 渐进式演进:从简单开始,根据需要逐步引入模式
  3. 3. Pythonic优先:充分利用Python语言特性,避免生搬硬套
  4. 4. 可测试性设计:让代码易于测试,便于持续改进
  5. 5. 团队协作规范:建立统一的命名、文档和审查标准

6.2 持续改进机制

6.2.1 设计模式使用回顾

定期回顾设计模式的使用情况:

classDesignPatternReview:
"""设计模式使用回顾"""

def__init__(self, codebase_path):
self.path = codebase_path

defcollect_pattern_usage(self):
"""收集模式使用情况"""
        patterns = {
"factory"self._detect_factory_pattern,
"strategy"self._detect_strategy_pattern,
"observer"self._detect_observer_pattern,
"decorator"self._detect_decorator_pattern
        }

        usage_stats = {}
for pattern_name, detector in patterns.items():
            count = detector()
            usage_stats[pattern_name] = count

return usage_stats

defgenerate_recommendations(self, usage_stats):
"""生成改进建议"""
        recommendations = []

# 检查过度使用
        total_patterns = sum(usage_stats.values())
if total_patterns > 50:  # 假设阈值
            recommendations.append("考虑简化设计,减少模式使用")

# 检查特定模式使用
if usage_stats.get("singleton"0) > 10:
            recommendations.append("单例模式使用过多,考虑其他方案")

return recommendations

6.2.2 性能基准测试

定期运行性能测试,确保模式引入没有负面影响:

import time
import statistics
from functools import wraps

classPerformanceBenchmark:
"""设计模式性能基准测试"""

def__init__(self):
self.results = {}

defbenchmark(self, func, *args, iterations=1000, **kwargs):
"""运行基准测试"""
        times = []

for _ inrange(iterations):
            start = time.perf_counter()
            func(*args, **kwargs)
            end = time.perf_counter()
            times.append(end - start)

        stats = {
"min"min(times) * 1000,  # 毫秒
"max"max(times) * 1000,
"mean": statistics.mean(times) * 1000,
"median": statistics.median(times) * 1000,
"stddev": statistics.stdev(times) * 1000iflen(times) > 1else0
        }

return stats

defcompare_implementations(self, implementations):
"""比较不同实现"""
        comparison = {}

for name, (func, args, kwargs) in implementations.items():
            stats = self.benchmark(func, *args, **kwargs)
            comparison[name] = stats

return comparison

# 使用示例
deftraditional_strategy():
# 传统策略模式实现
pass

defpythonic_strategy():
# Pythonic策略模式实现
pass

benchmark = PerformanceBenchmark()
results = benchmark.compare_implementations({
"traditional": (traditional_strategy, [], {}),
"pythonic": (pythonic_strategy, [], {})
})

最新文章

随机文章

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-04-28 15:33:31 HTTP/2.0 GET : https://f.mffb.com.cn/a/486566.html
  2. 运行时间 : 0.106332s [ 吞吐率:9.40req/s ] 内存消耗:4,721.83kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=0af1cd2f424eb9202195dbc217a88790
  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.000426s ] mysql:host=127.0.0.1;port=3306;dbname=f_mffb;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.000727s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.000375s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.000279s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.000481s ]
  6. SELECT * FROM `set` [ RunTime:0.000200s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.000495s ]
  8. SELECT * FROM `article` WHERE `id` = 486566 LIMIT 1 [ RunTime:0.004764s ]
  9. UPDATE `article` SET `lasttime` = 1777361611 WHERE `id` = 486566 [ RunTime:0.009078s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 66 LIMIT 1 [ RunTime:0.000389s ]
  11. SELECT * FROM `article` WHERE `id` < 486566 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.000503s ]
  12. SELECT * FROM `article` WHERE `id` > 486566 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.000413s ]
  13. SELECT * FROM `article` WHERE `id` < 486566 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.001286s ]
  14. SELECT * FROM `article` WHERE `id` < 486566 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.000667s ]
  15. SELECT * FROM `article` WHERE `id` < 486566 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.000874s ]
0.108101s