JAVA设计模式学习24——访问者模式

阅读更多

访问者(Visitor)模式:封装一些作用于某种数据结构中的各元素的操作,它可以在不改变这个数据结构的前提下定义作用于这些元素的新的操作。访问者模式的结构如下:

JAVA设计模式学习24——访问者模式_第1张图片
 通过上图可以看到他有如下角色:

抽象访问者(Visitor)角色:定义接口,声明一个或多个访问操作。 
具体访问者(ConcreteVisitor)角色:实现抽象访问者所声明的接口,也就是抽象访问者所声明的各个访问操作。 
抽象元素(Visitable)角色:声明一个接受操作,接受一个访问者对象作为一个参数。 
具体元素结点(ConcreteElement)角色:实现抽象结点所规定的接受操作。 
数据结构对象(ObjectStructure)角色:可以遍历结构中的所有元素,提供一个接口让访问者对象都可以访问每一个元素。 
模拟代码如下:
package visitor;
/**
 * 
 *作者:alaric
 *时间:2013-9-13下午11:31:28
 *描述:抽象访问者
 */
public interface Visitor {

	public void visit(ConcreteElementB able );
	public void visit(ConcreteElementA able );
}
 
package visitor;
/**
 * 
 *作者:alaric
 *时间:2013-9-13下午11:31:46
 *描述:抽象角色元素
 */
public interface Visitable {
	
	public void accept(Visitor v);

}
 
package visitor;
/**
 * 
 *作者:alaric
 *时间:2013-9-13下午11:33:29
 *描述:具体访问者A
 */
public class ConcreteVisitorA implements Visitor{

	@Override
	public void visit(ConcreteElementB able) {
		able.operate();
	}

	@Override
	public void visit(ConcreteElementA able) {
		// TODO Auto-generated method stub
		able.operate();
	}
	

}
 
package visitor;

/**
 * 
 *作者:alaric
 *时间:2013-9-13下午11:32:55
 *描述:具体访问者B
 */
public class ConcreteVisitorB implements Visitor{

	@Override
	public void visit(ConcreteElementB able) {
		able.operate();
	}

	@Override
	public void visit(ConcreteElementA able) {
		// TODO Auto-generated method stub
		able.operate();
	}

	

}
 
package visitor;
/**
 * 
 *作者:alaric
 *时间:2013-9-13下午11:34:02
 *描述:具体元素A
 */
public class ConcreteElementA implements Visitable {

	@Override
	public void accept(Visitor v) {
		v.visit(this);
	}

	public void operate(){
		System.out.println("ConcreteElementA ....");
	}
}
 
package visitor;
/**
 * 
 *作者:alaric
 *时间:2013-9-13下午11:33:40
 *描述:具体元素B
 */
public class ConcreteElementB implements Visitable {

	@Override
	public void accept(Visitor v) {
		v.visit(this);
	}

	public void operate(){
		System.out.println("ConcreteElementB ....");
	}
}
 
package visitor;

import java.util.ArrayList;
import java.util.List;
/**
 * 
 *作者:alaric
 *时间:2013-9-13下午11:34:22
 *描述:客户端
 */
public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Visitor v1 = new ConcreteVisitorA();
		List list = new ArrayList<>();
		list.add(new ConcreteElementA());
		list.add(new ConcreteElementB());
		
		for(Visitable able :list){
			able.accept(v1);
		}
	
	}

}
 

看了很多设计模式的书,讲访问者设计模式都要提到一个概念“双重分派”,所谓“分派”简单理解就是根据类的特性,特征进行选择,这些选择都是程序语言设计的特征,比如多态(重载,重写)等等,我个人不太注重概念,只要深入掌握面向对象的基础就很好理解了。

 

设计模式相对其他模式来说结构有点复杂,上面是访问者模式的模拟实现,为了利于学习找了个真实的例子。dom4j里面利用访问者模式来对xml文档进行逐个节点访问,所有文档的对象的父类接口都是Node,对于不同类型的文档对象又做了不同的抽象,所有可能访问的节点如Visitor类中所示,dom4j中定义的Visitor接口如下:

/*
 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
 *
 * This software is open source.
 * See the bottom of this file for the licence.
 */

package org.dom4j;

/**
 * 

* Visitor is used to implement the Visitor * pattern in DOM4J. An object of this interface can be passed to a * Node which will then call its typesafe methods. Please refer * to the Gang of Four book of Design Patterns for more details on the * Visitor pattern. *

* *

* This site * has further discussion on design patterns and links to the GOF book. This link describes the * Visitor pattern in detail. *

* * @author James Strachan * @version $Revision: 1.8 $ */ public interface Visitor { /** *

* Visits the given Document *

* * @param document * is the Document node to visit. */ void visit(Document document); /** *

* Visits the given DocumentType *

* * @param documentType * is the DocumentType node to visit. */ void visit(DocumentType documentType); /** *

* Visits the given Element *

* * @param node * is the Element node to visit. */ void visit(Element node); /** *

* Visits the given Attribute *

* * @param node * is the Attribute node to visit. */ void visit(Attribute node); /** *

* Visits the given CDATA *

* * @param node * is the CDATA node to visit. */ void visit(CDATA node); /** *

* Visits the given Comment *

* * @param node * is the Comment node to visit. */ void visit(Comment node); /** *

* Visits the given Entity *

* * @param node * is the Entity node to visit. */ void visit(Entity node); /** *

* Visits the given Namespace *

* * @param namespace * is the Namespace node to visit. */ void visit(Namespace namespace); /** *

* Visits the given ProcessingInstruction *

* * @param node * is the ProcessingInstruction node to visit. */ void visit(ProcessingInstruction node); /** *

* Visits the given Text *

* * @param node * is the Text node to visit. */ void visit(Text node); } /* * Redistribution and use of this software and associated documentation * ("Software"), with or without modification, are permitted provided that the * following conditions are met: * * 1. Redistributions of source code must retain copyright statements and * notices. Redistributions must also contain a copy of this document. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. The name "DOM4J" must not be used to endorse or promote products derived * from this Software without prior written permission of MetaStuff, Ltd. For * written permission, please contact [email protected]. * * 4. Products derived from this Software may not be called "DOM4J" nor may * "DOM4J" appear in their names without prior written permission of MetaStuff, * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd. * * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org * * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved. */
 dom4j里面有个缺省的访问者(Visitor)的实现VisitorSupport,我们解析一个文档只需继承这个类,然后重写visit方法即可。一个简单的类图表示dom4j是怎么利用visitor设计模式的,如下图:
JAVA设计模式学习24——访问者模式_第2张图片
上图中Node的继承接口和实现类 还有很多这里就只标示了CharacterData及其他的子类。我只需要写XXX_Visitor就可以了。接下来我们写个例子看看:
我们要解析的XML如下:


      
	    1
		Test
	  
      
	    2
		Test2
	   
  我们写个客户端测试,为了简单,把Visitor作为内部类,直接就一个类完成,代码如下:  
package com.alaric.dom4j;

import java.io.File;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;

public class Dom4jTest {


    public class MyVisitor extends VisitorSupport {

        public void visit(Attribute node){
          System.out.println("属性 : "+node.getName()+" = "+node.getValue());
        }

        public void visit(Element node){
          if(node.isTextOnly()){
            System.out.println("节点: "+node.getName()+" = "+node.getText());
          }else{
            System.out.println("节点:"+node.getName());
          }
        }
    }


    public static void main(String[] args) throws Exception {

        SAXReader saxReader=new SAXReader();
        File file=new File("d:\\test.xml");
        try{
          Document doc=saxReader.read(file);
          doc.accept(new Dom4jTest(). new MyVisitor());
        }catch(DocumentException de){
          de.printStackTrace();
        }

    }

}
 

运行结果:

节点:table

属性 : name = test

节点:rows

节点:row

节点: id = 1

节点: test = Test

节点:row

节点: id = 2

节点: test = Test2

 

可以看出把xml节点顺序的访问了一边。每个人可以根据不同的xml来实现自己的Visitor,不论怎么写都可以遍历出你所有的节点,这就是visitor的厉害之处。访问者模式也不是万能的,他的缺点是当数据结构变化时,他的visitor接口及其实现都要改变。所以访问者模式不能使用在经常变化的数据接口上。在Gof的设计模式中,有以下情形可以考虑使用设计模式:

 

1、一个对象结构包含很多类对象,它们有不同的接口,而你想对这些对象实施一些依赖于其具体类的操作。
2、需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而你想避免让这些操作“污染”这些对象的类。Visitor使得你可以将相关的操作集中起来定义在一个类中。
3、当该对象结构被很多应用共享时,用Visitor模式让每个应用仅包含需要用到的操作。
4、 定义对象结构的类很少改变,但经常需要在此结构上定义新的操作。改变对象结构类需要重定义对所有访问者的接口,这可能需要很大的代价。如果对象结构类经常改变,那么可能还是在这些类中定义这些操作较好。

这些个人看来都是建议,项目中还要具体问题具体分析了。

 

设计模式系列目录:

JAVA设计模式学习1——统一建模语言UML

JAVA设计模式学习2——面向对象设计原则

JAVA设计模式学习3——设计模式概述

JAVA设计模式学习4——简单工厂模式

JAVA设计模式学习5——工厂方法模式

JAVA设计模式学习6——抽象工厂模式

JAVA设计模式学习7——单例模式

JAVA设计模式学习8——建造模式

JAVA设计模式学习9——原型模式

JAVA设计模式学习10——组合模式

JAVA设计模式学习11——适配器模式

JAVA设计模式学习12——装饰器模式

JAVA设计模式学习13——代理模式

JAVA设计模式学习14——享元模式

JAVA设计模式学习15——门面模式

JAVA设计模式学习16——桥梁模式

JAVA设计模式学习17——策略模式

JAVA设计模式学习18——模版方法模式

JAVA设计模式学习19——观察者模式

JAVA设计模式学习20——责任链模式

JAVA设计模式学习21——命令模式

JAVA设计模式学习22——备忘录模式

JAVA设计模式学习23——状态模式

JAVA设计模式学习24——访问者模式

JAVA设计模式学习25——迭代器模式

  • JAVA设计模式学习24——访问者模式_第3张图片
  • 大小: 21.6 KB
  • JAVA设计模式学习24——访问者模式_第4张图片
  • 大小: 86.1 KB
  • 查看图片附件

你可能感兴趣的:(访问者模式,visitor)