網站首頁 編程語言 正文
假設要實現一個存放多種類型數據結構的對象,比如一個存放算術操作數和操作符的樹結點,需要存放包含一元操作符、二元操作符和數字類型的結點
class Node: ? ? pass class UnaryOperator(Node): ? ? def __init__(self, operand): ? ? ? ? self.operand = operand class BinaryOperator(Node): ? ? def __init__(self, left, right): ? ? ? ? self.left = left ? ? ? ? self.right = right class Add(BinaryOperator): ? ? pass class Sub(BinaryOperator): ? ? pass class Mul(BinaryOperator): ? ? pass class Div(BinaryOperator): ? ? pass class Negative(UnaryOperator): ? ? pass class Number(Node): ? ? def __init__(self, value): ? ? ? ? self.value = value
執行運算需要這樣調用:
# 假設運算式子:2 - (2+2) * 2 / 1 = 2-(8) = -6.0 t1 = Add(Number(2), Number(2)) t2 = Mul(t1, Number(2)) t3 = Div(t2, Number(1)) t4 = Sub(Number(2), t3)
或者這樣調用:
t5 = Sub(Number(2), Div(Mul(Add(Number(2), Number(2)), Number(2)), Number(1)))
這樣子需要執行多次類的調用,極不易讀寫且冗長,有沒有一種方法讓調用更加通用,訪問變得簡單呢。這里使用訪問者模式可以達到這樣的目的。
訪問者模式能夠在不改變元素所屬對象結構的情況下操作元素,讓調用或調用者(caller)的方式變得簡單,這種操作常見于的士公司操作,當一個乘客叫了一輛的士時,的士公司接收到了一個訪問者,并分配一輛的士去接這個乘客。
首先定義一個訪問者結點類VisitorNode
,實現最基本的訪問入口,任何訪問的方式都需要繼承這個訪問者結點類,并通過這個訪問者結點類的visit()方法來訪問它的各種操作
# 訪問者節點的基類 class NodeVisitor: ? ? def visit(self, node): ? ? ? ? if not isinstance(node, Node): ?# 不是Node對象時當做一個值返回,如果有其他情況可以根據實際來處理 ? ? ? ? ? ? return node ? ? ? ? self.meth = "visit_" + type(node).__name__.lower() ?# type(node)也可以換成node.__class__(只要node.__class__不被篡改) ? ? ? ? meth = getattr(self, self.meth, None) ? ?? ??? ?if meth is None: ? ? ? ? ? ? meth = self.generic_visit ? ? ? ? return meth(node) ? ? def generic_visit(self, node): ? ? ? ? raise RuntimeError(f"No {self.meth} method") # (一種)訪問者對應的類 class Visitor(NodeVisitor): ? ? """ ? ? 方法的名稱定義都要與前面定義過的結點類(Node)的名稱保證一致性 ? ? """ ? ? def visit_add(self, node): ? ? ? ? return self.visit(node.left) + self.visit(node.right) ? ? def visit_sub(self, node): ? ? ? ? return self.visit(node.left) - self.visit(node.right) ? ? def visit_mul(self, node): ? ? ? ? return self.visit(node.left) * self.visit(node.right) ? ? def visit_div(self, node): ? ? ? ? return self.visit(node.left) / self.visit(node.right) ? ? def visit_negative(self, node): ?# 如果class Negative 命名-> class Neg,那么 def visit_negative 命名-> def visit_neg ? ? ? ? return -self.visit(node.operand) ? ? def visit_number(self, node): ? ? ? ? return node.value
這里的meth = getattr
(self, self.meth, None)使用了字符串調用對象方法,self.meth動態地根據各類Node類(Add, Sub, Mul…)的名稱定義了對應于類Visitor中的方法(visit_add, visit_sub, visit_mul…)簡化了訪問入口的代碼,當沒有獲取到對應的方法時會執行generic_visit()并拋出RuntimeError的異常提示訪問過程中的異常
如果需要添加一種操作,比如取絕對值,只需要定義一個類class Abs(Unaryoperator): pass并在類Visitor中定義一個visit_abs
(self, node)方法即可,不需要做出任何多余的修改,更不需要改變存儲的結構
這里visit()方法調用了visit_xxx()方法,而visit_xxx()可能也調用了visit(),本質上是visit()的循環遞歸調用,當數據量變大時,效率會變得很慢,且遞歸層次過深時會導致超過限制而失敗,而下面介紹的就是利用棧和生成器來消除遞歸提升效率的實現訪問者模式的方法
import types class Node: ? ? pass class BinaryOperator(Node): ? ? def __init__(self, left, right): ? ? ? ? self.left = left ? ? ? ? self.right = right class UnaryOperator(Node): ? ? def __init__(self, operand): ? ? ? ? self.operand = operand class Add(BinaryOperator): ? ? pass class Sub(BinaryOperator): ? ? pass class Mul(BinaryOperator): ? ? pass class Div(BinaryOperator): ? ? pass class Negative(UnaryOperator): ? ? pass class Number(Node): ? ? def __init__(self, value): ?# 與UnaryOperator區別僅命名不同 ? ? ? ? self.value = value class NodeVisitor: ? ? def visit(self, node): ? ? ? ? # 使用棧+生成器來替換原來visit()的遞歸寫法 ? ? ? ? stack = [node] ? ? ? ? last_result = None ?# 執行一個操作最終都會返回一個值 ? ? ? ? while stack: ? ? ? ? ? ? last = stack[-1] ? ? ? ? ? ? try: ? ? ? ? ? ? ? ? if isinstance(last, Node): ? ? ? ? ? ? ? ? ? ? stack.append(self._visit(stack.pop())) ? ? ? ? ? ? ? ? elif isinstance(last, types.GeneratorType): ? # GeneratorType會是上一個if返回的對象,這個對象會返回兩個node執行算術之后的結果 ? ? ? ? ? ? ? ? ? ? # 如果是生成器,不pop掉,而是不斷send,直到StopIteration ? ? ? ? ? ? ? ? ? ? # 如果last_result不是None,這個值會給回到生成器(例如2被visit_add()的左值接收到) ? ? ? ? ? ? ? ? ? ? stack.append(last.send(last_result)) ? ? ? ? ? ? ? ? ? ? last_result = None ? ? ? ? ? ? ? ? else: ? # 計算結果是一個值 ? ? ? ? ? ? ? ? ? ? last_result = stack.pop() ? ? ? ? ? ? except StopIteration: ? # 生成器yield結束 ? ? ? ? ? ? ? ? stack.pop() ? ? ? ? return last_result ? ? def _visit(self, node): ? ? ? ? self.method_name = "visit_" + type(node).__name__.lower() ? ? ? ? method = getattr(self, self.method_name, None) ? ? ? ? if method is None: ? ? ? ? ? ? self.generic_visit(node) ? ? ? ? return method(node) ? ? def generic_visit(self, node): ? ? ? ? raise RuntimeError(f"No {self.method_name} method") class Visitor(NodeVisitor): ? ? def visit_add(self, node): ? ? ? ? yield (yield node.left) + (yield node.right) ? ?# node.left和node.right都可能是Node ? ? def visit_sub(self, node): ? ? ? ? yield (yield node.left) - (yield node.right) ? ? def visit_mul(self, node): ? ? ? ? yield (yield node.left) * (yield node.right) ? ? def visit_div(self, node): ? ? ? ? yield (yield node.left) / (yield node.right) ? ? def visit_negative(self, node): ? ? ? ? yield -(yield node.operand) ? ? def visit_number(self, node): ? ? ? ? return node.value
測試是否還會引起超過遞歸層數的異常
def test_time_cost(): ? ? import time ? ? s = time.perf_counter() ? ? a = Number(0) ? ? for n in range(1, 100000): ? ? ? ? a = Add(a, Number(n)) ? ? v = Visitor() ? ? print(v.visit(a)) ? ? print(f"time cost:{time.perf_counter() - s}")
輸出正常,沒有問題
4999950000
time cost:0.9547078
最后琢磨出了一個似乎可以作為替代的方法:
clas Node: ? ? psass class UnaryOperator(Node): ? ? def __init__(self, operand): ? ? ? ? self.operand = operand class BinaryOperator(Node): ? ? def __init__(self, left, right): ? ? ? ? self.left = left ? ? ? ? self.right = right class Add(BinaryOperator): ? ? def __init__(self, left, right): ? ? ? ? super().__init__(left, right) ? ? ? ? self.value = self.left.value + self.right.value ? ? pass class Sub(BinaryOperator): ? ? def __init__(self, left, right): ? ? ? ? super().__init__(left, right) ? ? ? ? self.value = self.left.value - self.right.value ? ? pass class Mul(BinaryOperator): ? ? def __init__(self, left, right): ? ? ? ? super().__init__(left, right) ? ? ? ? self.value = self.left.value * self.right.value ? ? pass class Div(BinaryOperator): ? ? def __init__(self, left, right): ? ? ? ? super().__init__(left, right) ? ? ? ? self.value = self.left.value / self.right.value ? ? pass class Negative(UnaryOperator): ? ? def __init__(self, operand): ? ? ? ? super().__init__(operand) ? ? ? ? self.value = -self.operand.value ? ? pass class Number(Node): ? ? def __init__(self, value): ? ? ? ? self.value = value
運行測試:
def test_time_cost(): ? ? import time ? ? s = time.perf_counter() ? ? a = Number(0) ? ? for n in range(1, 100000): ? ? ? ? a = Add(a, Number(n)) ? ? print(a.value) ? ? print(time.perf_counter() - s)
輸出:
4999950000
0.2506986
原文鏈接:https://blog.csdn.net/Moelimoe/article/details/123755397
相關推薦
- 2023-04-14 使用?React?hooks?實現類所有生命周期_React
- 2022-10-18 Go語言TCP從原理到代碼實現詳解_Golang
- 2022-11-02 python調用subprocess模塊實現命令行操作控制SVN的方法_python
- 2022-06-02 EasyX實現自由落體小球_C 語言
- 2022-12-11 詳解Android?GLide圖片加載常用幾種方法_Android
- 2022-04-15 c語言?指針零基礎講解_C 語言
- 2022-11-19 Python教程之無限迭代器的使用詳解_python
- 2022-07-08 圖文詳解Nginx多種匹配方式_nginx
- 最近更新
-
- 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同步修改后的遠程分支