Visitor访问者模式---------学习dom4j时遇到的顺便拿来交流

    dom4j遍历xml文档树有种很特别的方式就是访问者(Visitor)模式,初次接触Visitor模式,写出个人理解大家交流!
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();
    }

}


    我们使用了 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);
}
 

下来是四个被访问的类型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);
    }

}

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);

    }

}

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);
    }

}

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);
    }

}


下来定义一个访问者 Visitor 接口,它可以访问 Integer,String,Person(VO 对象 ),Collection 类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值