ACYCLIC VISITOR模式解决Shape排序问题
来源:互联网 发布:开源旅游cms 编辑:程序博客网 时间:2024/06/05 15:56
Robert C Martin 在他OCP(Open/Closed Principle,可以参考<<Agile Principles Patterns and Practices in C#>>)的教程里,以Shape作为例子,介绍如何运用OCP。
但是有一个问题没有给答案,只是说:
“It is possible to solve this problem by using the ACYCLIC VISITOR pattern described in Chapter 35. Showing that solution now
would be getting ahead of ourselves a bit. I'll remind you to come back here at the end of that chapter.”
我刚好看过ACYCLIC VISITOR模式,对这个模式如何解决Shape排序输出摸不到头脑,还专门在CSDN的C#论坛发帖。
自己动手,丰衣足食,我能想到的解决方案如下:
将Shape和OrderedObject分开,可以避免违反SRP。
方案的关键是可以在MyShapeVisitor里面设置每个Shape子类的优先级,如果要修改,只改这一个类即可。
如果Shape有新的子类,如Triangle,只要添加一个新的接口TriangleVisitor,在MyShapeVisitor里面
实现这个新接口,同时按需调整各Shape子类的优先级就可以了。
0 0
- ACYCLIC VISITOR模式解决Shape排序问题
- Visitor模式与Acyclic Visitor
- 再议访问者模式 - Visitor vs Acyclic Visitor
- Loki库里的Acyclic Visitor
- Visitor----------模式
- Visitor模式
- visitor模式
- Visitor模式
- Visitor模式
- Visitor 模式
- Visitor模式
- visitor模式
- Visitor模式
- Visitor模式
- Visitor模式
- visitor模式
- Visitor模式
- Visitor模式
- Sublime Text 2的Emmet插件使用简介
- 服务器端校验(更重要)和客户端校验
- poj2081 Recaman's Sequence
- Android实现微信支付功能
- rabbitMq使用(mac平台)
- ACYCLIC VISITOR模式解决Shape排序问题
- 【第六章】 AOP 之 6.2 AOP的HelloWorld ——跟我学spring3
- C++Primer::头文件设计基本原则 与 预处理器介绍
- DAO
- fastjson
- JSP页面输入数字提交后产生异常
- 解决虚拟机提示VMware Workstation cannot connect to the virtual machine的问题
- 剑指offer之面试题21包含min函数的栈
- 事件分发系列—View中的dispatchTouchEvent和onTouchEvent分析