- 浏览: 87311 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
Ginger90:
其实大部分功能和帆软报表差不多,但是帆软报表编写起来更方便一些 ...
iReport+jasperReport之JFreeChart(图表报表) -
tiancaishan:
这只是最简单的主数据源,那子数据源怎么设置呢?
iReport+jasperReport之CSV、XML数据源 -
wangqiangcattsoft:
楼上源码只看了一半
Struts2整合JasperReport预览HTML格式不显示图片问题解决方案 -
tuan232311009:
不错!
黑莓开发学习入门系列,自己动手实现一个日历软件(六)
Visitor访问者模式---------学习dom4j时遇到的顺便拿来交流
dom4j遍历xml文档树有种很特别的方式就是访问者(Visitor)模式,初次接触Visitor模式,写出个人理解大家交流!
Visitor访问者模式定义:作用于某个对象树中各个对象的操作. 它可以使你在不改变这些对象树本身的情况下,定义作用于这些对象树各个节点的新操作。
先看以下代码:Person为简单的vo类
我们使用了 instanceof来判断 Object对象 o 的类型,这样做的缺点是代码中If/else if 很繁琐,而JDK中的范型又限制了只能使用相同的类型,这时Vistor访问模式派上用场了。
当我们要访问Collection的每一个Element(被访问者)时,定义一个accept操作使其具有可被访问性,我们定义一个Visiable接口,使Collection的每一个Element继承这个接口,实现自身的访问操作
下来定义一个访问者Visitor接口,它可以访问Integer,String,Person(VO对象),Collection类型
Visitor访问者模式定义:作用于某个对象树中各个对象的操作. 它可以使你在不改变这些对象树本身的情况下,定义作用于这些对象树各个节点的新操作。
先看以下代码:Person为简单的vo类
package org.bulktree.visitor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/** *//**
*
* @author bulktree Email: laoshulin@gmail.com
* @date Aug 10, 2008
*/
public class ReadCollection {
private Collection c = null;
ReadCollection() {
/**//*
* 准备数据-String对象-Person对象-Integer对象-List对象
*/
String str = "bulktree.laoshulin";
Person person = new Person("bulktree", "22", "M");
Integer a = new Integer(99);
/**//*
* 使用范型
*/
List<String> list = new ArrayList<String>();
list.add("BULKTREE");
list.add("LAOSHULIN");
list.add("OAKERTREE");
c = new ArrayList();
c.add(str);
c.add(person);
c.add(a);
c.add(list);
}
/** *//**
* 遍历Collection中的每一个对象并打印
*/
public void testCollection() {
Iterator iter = getCollection().iterator();
while (iter.hasNext()) {
Object o = iter.next();
if (o instanceof String) {
System.out.println("String--> " + o.toString());
} else if (o instanceof Person) {
readPerson((Person) o);
} else if (o instanceof Integer) {
Integer inta = (Integer) o;
System.out.println(inta.intValue());
} else if (o instanceof List) {
readList((List) o);
}
}
}
public Collection getCollection() {
return c;
}
private void readPerson(Person person) {
System.out.println("person-name-> " + person.getName());
System.out.println("person-age-> " + person.getAge());
System.out.println("person-sex-> " + person.getSex());
}
private void readList(List<String> list) {
/**//*
* 增强的for循环
*/
for (String s : list) {
System.out.println(s);
}
}
public static void main(String[] args) {
new ReadCollection().testCollection();
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/** *//**
*
* @author bulktree Email: laoshulin@gmail.com
* @date Aug 10, 2008
*/
public class ReadCollection {
private Collection c = null;
ReadCollection() {
/**//*
* 准备数据-String对象-Person对象-Integer对象-List对象
*/
String str = "bulktree.laoshulin";
Person person = new Person("bulktree", "22", "M");
Integer a = new Integer(99);
/**//*
* 使用范型
*/
List<String> list = new ArrayList<String>();
list.add("BULKTREE");
list.add("LAOSHULIN");
list.add("OAKERTREE");
c = new ArrayList();
c.add(str);
c.add(person);
c.add(a);
c.add(list);
}
/** *//**
* 遍历Collection中的每一个对象并打印
*/
public void testCollection() {
Iterator iter = getCollection().iterator();
while (iter.hasNext()) {
Object o = iter.next();
if (o instanceof String) {
System.out.println("String--> " + o.toString());
} else if (o instanceof Person) {
readPerson((Person) o);
} else if (o instanceof Integer) {
Integer inta = (Integer) o;
System.out.println(inta.intValue());
} else if (o instanceof List) {
readList((List) o);
}
}
}
public Collection getCollection() {
return c;
}
private void readPerson(Person person) {
System.out.println("person-name-> " + person.getName());
System.out.println("person-age-> " + person.getAge());
System.out.println("person-sex-> " + person.getSex());
}
private void readList(List<String> list) {
/**//*
* 增强的for循环
*/
for (String s : list) {
System.out.println(s);
}
}
public static void main(String[] args) {
new ReadCollection().testCollection();
}
}
我们使用了 instanceof来判断 Object对象 o 的类型,这样做的缺点是代码中If/else if 很繁琐,而JDK中的范型又限制了只能使用相同的类型,这时Vistor访问模式派上用场了。
当我们要访问Collection的每一个Element(被访问者)时,定义一个accept操作使其具有可被访问性,我们定义一个Visiable接口,使Collection的每一个Element继承这个接口,实现自身的访问操作
package org.bulktree.visitor;
/** *//**
* 可访问性--接收一个访问者
* @author bulktree Email: laoshulin@gmail.com
* @date Aug 10, 2008
*/
public interface Visitable {
public void accept(Visitor visitor);
}
/** *//**
* 可访问性--接收一个访问者
* @author bulktree Email: laoshulin@gmail.com
* @date Aug 10, 2008
*/
public interface Visitable {
public void accept(Visitor visitor);
}
下来是四个被访问的类型String,Integer,Person,Collection的实现类
package org.bulktree.visitor;
/** *//**
* 被访问者--String对象
* @author bulktree Email: laoshulin@gmail.com
* @date Aug 10, 2008
*/
public class StringElement implements Visitable {
private String str;
public StringElement(String str) {
this.str = str;
}
public String getStr() {
return str;
}
public void accept(Visitor visitor) {
visitor.visitString(this);
}
}
/** *//**
* 被访问者--String对象
* @author bulktree Email: laoshulin@gmail.com
* @date Aug 10, 2008
*/
public class StringElement implements Visitable {
private String str;
public StringElement(String str) {
this.str = str;
}
public String getStr() {
return str;
}
public void accept(Visitor visitor) {
visitor.visitString(this);
}
}
package org.bulktree.visitor;
/** *//**
* 被访问者--Integer对象
* @author bulktree Email: laoshulin@gmail.com
* @date Aug 10, 2008
*/
public class IntegerElement implements Visitable {
private Integer i;
public IntegerElement(Integer i) {
this.i = i;
}
public Integer getI() {
return i;
}
public void accept(Visitor visitor) {
visitor.visitInteger(this);
}
}
/** *//**
* 被访问者--Integer对象
* @author bulktree Email: laoshulin@gmail.com
* @date Aug 10, 2008
*/
public class IntegerElement implements Visitable {
private Integer i;
public IntegerElement(Integer i) {
this.i = i;
}
public Integer getI() {
return i;
}
public void accept(Visitor visitor) {
visitor.visitInteger(this);
}
}
package org.bulktree.visitor;
import java.util.Collection;
/** *//**
* 被访问者--Person对象
* @author bulktree Email: laoshulin@gmail.com
* @date Aug 10, 2008
*/
public class PersonElement implements Visitable{
private Person p;
public PersonElement(Person p) {
this.p = p;
}
public Person getP() {
return p;
}
public void accept(Visitor visitor) {
visitor.visitPerson(this);
}
}
import java.util.Collection;
/** *//**
* 被访问者--Person对象
* @author bulktree Email: laoshulin@gmail.com
* @date Aug 10, 2008
*/
public class PersonElement implements Visitable{
private Person p;
public PersonElement(Person p) {
this.p = p;
}
public Person getP() {
return p;
}
public void accept(Visitor visitor) {
visitor.visitPerson(this);
}
}
package org.bulktree.visitor;
import java.util.Collection;
import java.util.List;
/** *//**
* 被访问者--Collection对象
* @author bulktree Email: laoshulin@gmail.com
* @date Aug 10, 2008
*/
public class CollectionElement implements Visitable {
private Collection collection;
public CollectionElement(Collection collection) {
this.collection = collection;
}
public Collection getCollection() {
return collection;
}
public void accept(Visitor visitor) {
visitor.visitCollection(collection);
}
}
import java.util.Collection;
import java.util.List;
/** *//**
* 被访问者--Collection对象
* @author bulktree Email: laoshulin@gmail.com
* @date Aug 10, 2008
*/
public class CollectionElement implements Visitable {
private Collection collection;
public CollectionElement(Collection collection) {
this.collection = collection;
}
public Collection getCollection() {
return collection;
}
public void accept(Visitor visitor) {
visitor.visitCollection(collection);
}
}
下来定义一个访问者Visitor接口,它可以访问Integer,String,Person(VO对象),Collection类型
border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; padding-top: 4px; border-
发表评论
-
如何使用Static的变量和方法
2007-12-01 10:55 681如何使用Static的变量和方法有时你希望定义一个类成员 ... -
Transact-SQL简明语法
2007-12-01 11:02 983最简单的查询语法格式: SELECT <列名&g ... -
去北京面试时的题目(老师生日)微软面试题
2007-12-01 19:59 1136小明和小强都是张老师的学生,张老师的生日是 M月 N日, 2人 ... -
有点象24点游戏,说说你的想法
2007-12-01 23:02 701* 5、5、5、1怎么能算出24呢? * ... -
JDK5增强for循环的使用
2007-12-02 23:43 743JDK5增强for循环的使用,此语法有很大的缺陷,仅适用于有限 ... -
Java中的多态
2007-12-03 11:26 758在实际操作中,多态可以让我不用关心某个对象到底是什么具体类型, ... -
运用线程实现时间更新
2007-12-06 11:01 707线程的做的时间表,不知道合不合适,哪位还有更好的实现方式(ja ... -
生产者消费者问题(以面包为例)
2007-12-10 19:48 720package Thread;public class Pr ... -
数据库关系表解决方案
2007-12-26 21:45 696数据库关系的表之间定义一个连接,而所定义连接的方式由表之 ... -
Struts2表单请求流程(一)
2007-12-29 20:09 801Struts2框架出来有段时间了,终于开始学习Struts了, ... -
Struts2表单请求流程(二)— Struts2高级的POJO访问
2007-12-29 20:20 1897此文主要来自www.blogjava.net/Max的实例 S ... -
开源软件逐渐走进了我的生活
2008-03-18 21:58 704看看我们经常用的软件吧! 以前经常去找一些破解的软件用,没有那 ... -
Struts2中select/doubleselect标签数据显示
2008-04-02 08:46 1053最近做毕业设计用到Struts2 的标签库,遇到一些比较复 ... -
用commons-email-1.1.jar实现发邮件功能:
2008-04-07 09:47 1054今天在Apache网站上下载commons-lang j ... -
JFreeChart简单使用
2008-04-18 09:29 831JFreeChart is a free chart libr ... -
Spring2.5.3+Hibernate3.2+Struts2.0.11整合
2008-04-18 13:59 857Spring2.5.3+Hibernate3.2+Struts ... -
java截取字符串的一些常用处理
2008-08-01 09:06 923java对字符串处理功能强大,以下是本人实际开发中用到的一些总 ... -
dom4j操作xml基础
2008-08-03 15:39 923DOM是用与平台和语言无关的方式表示XML文档 ... -
office javaScript调试工具
2008-08-05 18:21 873项目前台需要写大量的js,寻求一种有效 ... -
dom4j操作xml基础--Visitor访问模式解析XML
2008-08-10 12:52 932废话不说了,看了上篇Visitor访问者模式--------- ...
相关推荐
设计模式C++学习之访问者模式(Visitor)
C#面向对象设计模式 (行为型模式) Visitor 访问者模式 视频讲座下载
C#面向对象设计模式纵横谈(24):(行为型模式) Visitor 访问者模式
访问者(Visitor)模式的定义:将作用于某种数据结构中的各元素的操作分离出来封装成独立的类,使其在不改变数据结构的前提下可以添加作用于这些元素的新的操作,为数据结构中的每个元素提供多种访问方式。...
访问者模式(Visitor) 用意:适用于数据结构相对未定的系统,把数据结构和作用于结构上的操作间的耦合解开。
设计模式 -访问者(Visitor)模式详解和应用.pdf
Visitor(访问者模式)属于行为型模式。意图:表示一个作用于某对象结构中的各元素的操作。
dom4j的例子 xpp3 dom4j说明 visitor解析
可以看到,要实现操作权转让到 Visitor,核心是元素必须实现一个 Accept 函数,将这个对象抛给 Visitor:从上面代码可以看出这样一条链路:Ele
访问者模式(Visitor Pattern)是GoF提出的23种设计模式中的一种,属于行为模式。它表示一个作用于某对象结构中的各元素的操作,它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作。从定义可以看出,...
dom4j实例详解 1 目录 1 一、dom4j介绍 2 二、使用dom4j创建xml文档 3 三、使用dom4j解析xml文件 5 1. 构建dom4j树 5 2. 获取节点 5 3. 获取属性 6 4. 使用XPath获取节点和属性 6 四、使用dom4j修改xml文件 7 五、...
主要介绍了iOS应用的设计模式开发中的Visitor访问者模式的实例,示例代码为传统的Objective-C,需要的朋友可以参考下
访问者模式(Java代码实现),博文附件
面向对象 北工大 课设 使用多种设计模式,实现的求值引擎,包括表达式树的构建
C#面向对象设计模式纵横谈(24):(行为型模式) Visitor 访问者模式.rar 李建忠主讲 上海祝成信息科技有限公司担任软件架构师,并讲授C#/.NET软件开发课程
表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。
赠送jar包:antlr4-runtime-4.2.jar; 赠送原API文档:antlr4-runtime-4.2-javadoc.jar; 赠送源代码:antlr4-runtime-4.2-sources.jar; 赠送Maven依赖信息文件:antlr4-runtime-4.2.pom; 包含翻译后的API文档:...
本资料来源于吕震宇博客,欢迎访问 http://www.cnblogs.com/zhenyulu/category/6930.html?Show=All, 经过整理,方便大家阅读。内容包括: 设计模式(22)-Strategy Pattern 设计模式(21)-Template Method ...
Visitor模式.docVisitor模式.doc