网站首页 > 博客文章 正文
前言
访问者模式,表示一个作用于某个对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。
代码
class Action:
def Get_Man_Conclusion(self, concrete_element_A):
pass
def Get_Woman_Conclusion(self, concrete_element_B):
pass
class Person:
def Accept(self, visitor : Action):
pass
class Man(Person):
def Accept(self, visitor : Action):
visitor.Get_Man_Conclusion(self)
class Woman(Person):
def Accept(self, visitor: Action):
visitor.Get_Woman_Conclusion(self)
class Success(Action):
def Get_Man_Conclusion(self, concrete_element_A):
print(f"When {type(concrete_element_A).__name__} is {type(self).__name__},there is often a great woman behind him")
def Get_Woman_Conclusion(self, concrete_element_B):
print(f"When {type(concrete_element_B).__name__} is {type(self).__name__},there is probably an unsuccessful man behind him")
class Failing(Action):
def Get_Man_Conclusion(self, concrete_element_A):
print(f"When {type(concrete_element_A).__name__} is {type(self).__name__},no one needs to persuade him to drink alcohol")
def Get_Woman_Conclusion(self, concrete_element_B):
print(f"When {type(concrete_element_B).__name__} is {type(self).__name__},with tears in his eyes, no one can persuade him")
class Amativeness(Action):
def Get_Man_Conclusion(self, concrete_element_A):
print(f"When {type(concrete_element_A).__name__} is {type(self).__name__},he pretends to understand everything he doesn’t understand")
def Get_Woman_Conclusion(self, concrete_element_B):
print(f"When {type(concrete_element_B).__name__} is {type(self).__name__},she understands and pretends not to understand")
class Object_Structure:
def __init__(self) -> None:
self.elements = []
def Attach(self, element : Person):
self.elements.append(element)
def Detach(self, element : Person):
self.elements.remove(element)
def Display(self, visitor):
for e in self.elements:
e.Accept(visitor)
if __name__ == '__main__':
o = Object_Structure()
o.Attach(Man())
o.Attach(Woman())
v1 = Success()
o.Display(v1)
v2 = Failing()
o.Display(v2)
v3 = Amativeness()
o.Display(v3)
运行结果:
通过Object_Structure去维护所有的Person子类。Display接口用于传入具体的状态,比如Success,Failing,Amativeness。对于每个行为都会实现Get_Man_Conclusion和Get_Woman_Conclusion。而这两个接口又分别对应Man和Woman类去实际调用它。代码中Success,Failing,Amativeness都是具体的访问者。如果新增一个如Man一样的元素类,需要同时修改所有访问者类。
总结
访问者模式的目的是要把处理从数据结构分离出来,让算法和数据结构可以分开。
优点:增加新的操作很容易,因为增加新的操作就意味着增加一个新的访问者。
缺点:使得增加新的数据结构变得困难了。比如新增一个元素类,你需要修改所有的访问者类。
猜你喜欢
- 2024-10-01 设计模式——备忘录模式(备忘录界面设计)
- 2024-10-01 设计模式 之 行为型模式(行为型设计模式特点)
- 2024-10-01 Go语言设计模式-访问者(go语言有设计模式吗)
- 2024-10-01 Spring Boot 实战:运用访问者模式灵活扩展订单处理逻辑
- 2024-10-01 visitor pattern 访问者模式(访问者模式工作原理)
- 2024-10-01 Aha!设计模式(105)-访问者模式(1)
- 2024-10-01 GOF设计模式(命令模式,访问者模式,迭代器模式,观察者模式)
- 2024-10-01 设计模式第2招第9式之解释器模式(解释器原理)
- 2024-10-01 设计模式第2招第1式之观察者模式(观察者设计模式的优点)
- 2024-10-01 「编程设计」访问者(Visitor)模式-补丁式为原体系增加新功能
你 发表评论:
欢迎- 07-07Xiaomi Enters SUV Market with YU7 Launch, Targeting Tesla with Bold Pricing and High-Tech Features
- 07-07Black Sesame Maps Expansion Into Robotics With New Edge AI Strategy
- 07-07Wuhan's 'Black Tech' Powers China's Cross-Border Push with Niche Electronics and Scientific Firepower
- 07-07Maven 干货 全篇共:28232 字。预计阅读时间:110 分钟。建议收藏!
- 07-07IT运维必会的30个工具(it运维工具软件)
- 07-07开源项目有你需要的吗?(开源项目什么意思)
- 07-07自动化测试早就跑起来了,为什么测试管理还像在走路?
- 07-07Cursor 最强竞争对手来了,专治复杂大项目,免费一个月
- 最近发表
-
- Xiaomi Enters SUV Market with YU7 Launch, Targeting Tesla with Bold Pricing and High-Tech Features
- Black Sesame Maps Expansion Into Robotics With New Edge AI Strategy
- Wuhan's 'Black Tech' Powers China's Cross-Border Push with Niche Electronics and Scientific Firepower
- Maven 干货 全篇共:28232 字。预计阅读时间:110 分钟。建议收藏!
- IT运维必会的30个工具(it运维工具软件)
- 开源项目有你需要的吗?(开源项目什么意思)
- 自动化测试早就跑起来了,为什么测试管理还像在走路?
- Cursor 最强竞争对手来了,专治复杂大项目,免费一个月
- Cursor 太贵?这套「Cline+OpenRouter+Deepseek+Trae」组合拳更香
- 为什么没人真的用好RAG,坑都在哪里? 谈谈RAG技术架构的演进方向
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)