做餐饮在环保局网站备案wordpress视频采集
2026/1/8 9:19:47 网站建设 项目流程
做餐饮在环保局网站备案,wordpress视频采集,做原型网站,微信小程序二维码生成器Python 中的适配器模式#xff08;Adapter Pattern#xff09; 适配器模式是一种结构型设计模式#xff0c;其核心目的是#xff1a; 将一个类的接口转换成客户端期望的另一个接口#xff0c;让原本由于接口不兼容而无法一起工作的类可以协同工作。 形象比喻#xff1a…Python 中的适配器模式Adapter Pattern适配器模式是一种结构型设计模式其核心目的是将一个类的接口转换成客户端期望的另一个接口让原本由于接口不兼容而无法一起工作的类可以协同工作。形象比喻就像电源适配器将欧标插头转换成美标插座它不改变原有功能只做接口转换。两种形式类适配器通过多重继承实现—— 在 Python 中较少用因为 Python 支持多继承但不推荐过度使用。对象适配器通过组合实现——Python 中最推荐的方式更灵活、更符合“组合优于继承”原则。典型应用场景使用第三方库但其接口与你的代码不匹配整合遗留系统老代码接口过时需要统一不同数据源的接口如不同 API 返回格式想复用现有类但接口不一致Python 示例对象适配器推荐假设我们有一个老的支付系统OldPaymentSystem它的方法是make_payment(amount)现在引入一个新的第三方支付网关NewPaymentGateway方法是pay(amount, currency)。我们想让客户端代码统一使用pay(amount, currency)接口。# 被适配的类Adaptee—— 老系统classOldPaymentSystem:defmake_payment(self,amount):print(fOld system: Processing payment of ${amount})returnTrue# 目标接口Target—— 客户端期望的接口classPaymentProcessor:defpay(self,amount,currencyUSD):raiseNotImplementedError# 适配器Adapter—— 通过组合将老系统适配到新接口classOldSystemAdapter(PaymentProcessor):def__init__(self,old_system:OldPaymentSystem):self.old_systemold_systemdefpay(self,amount,currencyUSD):# 转换接口忽略 currency老系统不支持直接调用 make_paymentprint(fAdapter: Converting pay({amount},{currency}) to make_payment({amount}))returnself.old_system.make_payment(amount)# 新的支付网关另一个实现classNewPaymentGateway(PaymentProcessor):defpay(self,amount,currencyUSD):print(fNew gateway: Processing{currency}{amount}payment securely)returnTrue# 客户端代码统一使用 PaymentProcessor 接口defprocess_payment(processor:PaymentProcessor,amount:float):processor.pay(amount,USD)# 使用示例if__name____main__:# 使用新网关new_gatewayNewPaymentGateway()process_payment(new_gateway,100.0)# 使用老系统通过适配器old_systemOldPaymentSystem()adapterOldSystemAdapter(old_system)process_payment(adapter,200.0)输出New gateway: Processing USD 100.0 payment securely Adapter: Converting pay(200.0, USD) to make_payment(200.0) Old system: Processing payment of $200.0客户端代码完全不需要知道底层是新网关还是老系统。类适配器示例了解即可不推荐classOldSystemClassAdapter(OldPaymentSystem,PaymentProcessor):defpay(self,amount,currencyUSD):# 直接调用继承来的 make_paymentreturnself.make_payment(amount)# 使用adapterOldSystemClassAdapter()adapter.pay(150.0)缺点Python 多继承容易导致复杂性且无法适配已有实例。更实用的例子JSON API 适配器假设有两个 API 返回不同格式的数据我们想统一成get_user_info()返回标准字典。importjson# 旧 API 返回 XML 字符串classLegacyUserAPI:deffetch_user(self,user_id):# 模拟返回 XMLreturnfuserid{user_id}/idnameJohn Doe/nameemailjohnexample.com/email/user# 新 API 返回 JSONclassModernUserAPI:defget_user(self,user_id):return{id:user_id,name:Jane Doe,email:janeexample.com}# 目标接口classUserService:defget_user_info(self,user_id):raiseNotImplementedError# 适配器将旧 API 转为标准接口classLegacyAPIAdapter(UserService):def__init__(self,legacy_api:LegacyUserAPI):self.legacy_apilegacy_apidefget_user_info(self,user_id):xml_dataself.legacy_api.fetch_user(user_id)# 简单解析 XML实际可用 xml.etree.ElementTreeimportre data{id:re.search(rid(.*?)/id,xml_data).group(1),name:re.search(rname(.*?)/name,xml_data).group(1),email:re.search(remail(.*?)/email,xml_data).group(1),}returndata# 客户端统一调用defdisplay_user(service:UserService,user_id):infoservice.get_user_info(user_id)print(fUser:{info[name]}({info[email]}))# 测试legacy_adapterLegacyAPIAdapter(LegacyUserAPI())modern_serviceModernUserAPI()display_user(legacy_adapter,123)# 通过适配器使用旧 API# display_user(modern_service, 456) # 如果也适配成 UserService适配器模式结构总结角色说明Target客户端期望的接口PaymentProcessorClient使用 Target 接口的代码Adaptee需要被适配的原有类OldPaymentSystemAdapter实现 Target内部持有 Adaptee优点解耦客户端与具体实现复用遗留代码符合开闭原则扩展新适配器不修改原有代码缺点引入额外类略微增加复杂度如果适配逻辑复杂可能影响性能Python 中的实用建议优先使用对象适配器组合在处理第三方库时非常常见如不同日志库、缓存客户端、消息队列等结合abc模块定义抽象目标接口更清晰很多时候可以用简单函数或装饰器实现轻量适配不必每次都建类如果你想看更多实际案例如适配不同数据库客户端、日志系统、缓存 Redis vs Memcached或者与其他模式结合使用欢迎继续问

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询