JiveJdon Community Forums
在线171人   首页   主题表   培训咨询   标签   查搜   注册    登陆 RSS
首页 » 论坛 » 设计模式、框架和架构
???en_US.forumThreadPrev.name??? 上一主题
RSS google yahoo 365Key网摘 CSDN网摘 添加到百度搜藏 POCO网摘 新浪ViVi QQ网摘
???en_US.forumThreadNext.name??? 下一主题
Go 共有 2 回复(1页) 阅读99次
 发表新帖子   回复该主题贴
xysniper



发表文章:
注册时间: 2007年11月03日
悄悄话
个人博客
在线? 当前离线
我要关注该作者发言
谁说visitor不可以添加新元素? 2007年02月08日 14:52 收藏关注本主题 到本帖网址 加入本帖到收藏夹 请用鼠标选择需要回复的文字再点按本回复键 回复该主题
标签     
这个visitor就可以随便添加,利用反射就可以做到了.
public abstract class Visitor {

final public void visit(Object object) {
Method method = getVisitMethod(object);
try {
method.invoke(this, new Object[] { object });
} catch (Exception e) {
String message = "invokeing method failed:visit("
+ object.getClass().getName() + ")";
throw new RuntimeException(message, e);
}
}

private Map visitMethods = new HashMap();

private Method getVisitMethod(Object object) {
//return (Method) visitMethods.get(object.getClass());
Iterator it = visitMethods.keySet().iterator();
for (; it.hasNext();) {
Class clazz = (Class) it.next();
if (clazz.isAssignableFrom(object.getClass())) {
return (Method) visitMethods.get(clazz);
}
}
throw new RuntimeException("method: visit(" + object.getClass().getName()
+ ") undefined in the class");
}

private Method visitMethod;

private void initVisitMethod() {
Method[] methods = getClass().getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (methods.getName().equals("visit")) {
Class[] paramTypes = methods.getParameterTypes();
if (paramTypes.length == 1) {
visitMethods.put(paramTypes[0], methods);
}
}
}
}

{
initVisitMethod();
}
}

具体的Visitor
public class PrintVisitor extends Visitor{

public void visit(Float f){
System.out.println("float:"+f);
}
public void visit(Date date){
System.out.println("date:"+date);
}
public void visit(String str){
System.out.println("string:"+str);
}
}

public class PrintVisitor2 extends Visitor{

public void visit(String str){
System.out.println("string2:"+str);
}
public void visit(List list){
System.out.println("list2:"+list);
}
}


public void testVisit(){
ArrayList list = new ArrayList();
list.add("a");
list.add("b");
Visitor v = new PrintVisitor();
v.visit("hello");
v.visit(new Date());
Visitor v1 = new PrintVisitor2();
v1.visit("hello");
v1.visit(list);
}
youngjiang



发表文章:
注册时间: 2007年02月26日
悄悄话
个人博客
在线? 当前离线
我要关注该作者发言
re:谁说visitor不可以添加新元素? 2007年03月09日 17:02 收藏关注本主题 到本帖网址 加入本帖到收藏夹 请用鼠标选择需要回复的文字再点按本回复键 回复该主题
兄弟,辛苦啦,顶一下下
Coolyu0916



发表文章:
注册时间: 2007年04月23日
悄悄话
个人博客
在线? 当前离线
我要关注该作者发言
re:谁说visitor不可以添加新元素? 2007年03月10日 22:44 收藏关注本主题 到本帖网址 加入本帖到收藏夹 请用鼠标选择需要回复的文字再点按本回复键 回复该主题
this is a Visitor???
标签     
共有 2 回复(1页) Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
正在读取,请等待...
查询本论坛内 回复超过的热门帖子
标题
 
粗体: [b]文本[/b] 斜体: [i]文本[/i] 下划线 [u]文本[/u] 插入网上的图片 [img]http://wwww.xxxx.com/img.ext[/img] 插入代码 [code]程序代码[/code]  插入url链接 [url]http://url[/url] / [url=http://url]URL加下滑线[/url] 上传图片 word文档 Txt等附件
内容
  提交时自动拷贝以上内容到剪贴板 Ctrl+V可取出;提问题前先查询标签列表

使用帮助 手机m.jdon.com RSS add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的软件架构社区 推荐FireFox或Chrome快速浏览本站
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-10 jdon.com

anti spam