網站首頁 編程語言 正文
一、適配器模式
適配器,顧名思義是一種萬能的接口,達到萬能轉換的效果。
適配器模式,定義一個適配器類,并且在該類中定義了適配器接口,這些適配接口能夠將原來由于接口不兼容而不能在一起工作的多種類型進行適配,使得它們能夠一同工作。
二、應用場景
三、代碼示例
實體角色:
目標接口(Target):定義提供給 Client
訪問的接口,可以是一個抽象類或接口,也可以是具體類。
待適配的類 / 適配者類(Adaptee):被適配的角色,它們已經存在了一些接口,是 Client 希望的業務方法,這些接口需要被適配。
適配器(Adapter):作為一個轉換器,對 Adaptee
和 Target
進行適配。
方式一
import abc class Mobile(metaclass=abc.ABCMeta): ? ? @abc.abstractmethod ? ? def call(self): ? ? ? ? pass class Xiaomi(Mobile): ? ? # 目標接口 ? ? def call(self): ? ? ? ? print(f"使用{self.__class__.__name__}打電話") class Huawei(Mobile): ? ? # 目標接口 ? ? def call(self): ? ? ? ? print(f"使用{self.__class__.__name__}打電話") # 待適配的類。新的手機品牌,打電話的接口與舊的 call 不一樣。 class Iphone: ? ? def dial(self): ? ? ? ? print(f"使用{self.__class__.__name__}打電話") # 待適配的類。新的手機品牌,打電話的接口與舊的 call 不一樣。 class Chuizi: ? ? def dial(self): ? ? ? ? print(f"使用{self.__class__.__name__}打電話") # 適配器,使得新的手機品牌打電話接口與舊的適配 class MobileAdapter(Mobile): ? ? def __init__(self, mobile): ? ? ? ? self.mobile = mobile ? ? def call(self): ? ? ? ? self.mobile.dial() if __name__ == "__main__": ? ? xiaomi = Xiaomi() ? ? xiaomi.call() ? ? huawei = Huawei() ? ? huawei.call() ? ? iphone = MobileAdapter(Iphone()) ? ? iphone.call() ? ? chuizi = MobileAdapter(Chuizi()) ? ? chuizi.call()
方式二
適配器類通過__dict__
將需要轉化的類的方法注冊到適配器,重載 __getattr__
使其在適配器函數查無方法的時候,執行 getattr 方法。
class A: ? ? def a(self): ? ? ? ? print("我是A類的a方法") class B: ? ? def b(self): ? ? ? ? print("我是B類的b方法") class C: ? ? def c(self): ? ? ? ? print("我是C類的c方法") class Adapter: ? ? def __init__(self, classname, method): ? ? ? ? self.classname = classname ? ? ? ? self.__dict__update = method ? ? def __getattr__(self, attr): ? ? ? ? return getattr(self.classname, attr) def test(): ? ? objects = [] ? ? AA = A() ? ? objects.append(Adapter(AA, dict(test=AA.a))) ? ? BB = B() ? ? objects.append(Adapter(BB, dict(test=BB.b))) ? ? CC = C() ? ? objects.append(Adapter(CC, dict(test=CC.c))) ? ? for obj in objects: ? ? ? ? obj.test() test()
原文鏈接:https://is-cloud.blog.csdn.net/article/details/122928395
相關推薦
- 2022-11-16 Python數據分析之matplotlib繪圖詳解_python
- 2024-07-18 Spring Security概述快速入門
- 2023-02-27 python定時任務sched庫用法簡單實例_python
- 2022-03-22 C++抽象數據類型介紹_C 語言
- 2022-09-26 android Recycleview的側滑點擊刪除功能實現
- 2023-07-02 Python利用scikit-learn實現近鄰算法分類的示例詳解_python
- 2023-01-31 C#實現批量壓縮和解壓縮的示例代碼_C#教程
- 2022-07-02 C++精要分析右值引用與完美轉發的應用_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支