`
阅读更多
    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类型

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-
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics