Java设计模式----访问者模式

模式动机

对于存储在一个集合中的对象,他们可能具有不同的类型(即使有一个公共的接口),对于该集合中的对象,可以接受一类称为访问者的对象来访问,不同的访问者其访问方式也有所不同。

定义

表示一个作用于某对象结构中的各元素的操作,它使我们可以在不改变各元素的类的前提下定义作用于这些元素的新操作。

结构图

访问者模式

基本代码

package visitor;

public interface Visitor {
    void visitConcreteElementA(ConcreteElementA concreteElementA);
    void visitConcreteElementB(ConcreteElementB concreteElementB);
}
package visitor;

public interface Element {
    void accept(Visitor visitor);
}
package visitor;

public class ConcreteElementA implements Element{
    public void accept(Visitor visitor) {
        visitor.visitConcreteElementA(this);
    }
}
package visitor;

public class ConcreteElementB implements Element {
    public void accept(Visitor visitor) {
        visitor.visitConcreteElementB(this);
    }
}
package visitor;

public class ConcreteVisitor1 implements Visitor{
    public void visitConcreteElementA(ConcreteElementA concreteElementA) {
        System.out.println(concreteElementA.getClass().getSimpleName()+"被"+this.getClass().getSimpleName()+"访问");
    }

    public void visitConcreteElementB(ConcreteElementB concreteElementB) {
        System.out.println(concreteElementB.getClass().getSimpleName()+"被"+this.getClass().getSimpleName()+"访问");
    }
}
package visitor;

public class ConcreteVisitor2 implements Visitor{
    public void visitConcreteElementA(ConcreteElementA concreteElementA) {
        System.out.println(concreteElementA.getClass().getSimpleName()+"被"+this.getClass().getSimpleName()+"访问");
    }

    public void visitConcreteElementB(ConcreteElementB concreteElementB) {
        System.out.println(concreteElementB.getClass().getSimpleName()+"被"+this.getClass().getSimpleName()+"访问");
    }
}
package visitor;

import java.util.ArrayList;
import java.util.List;

public class ObjectStructure {
    private List<Element> elements = new ArrayList<Element>();

    public void attach(Element element){
        elements.add(element);
    }

    public void detach(Element element){
        elements.remove(element);
    }

    public void accept(Visitor visitor){
        for (Element element:elements){
            element.accept(visitor);
        }
    }
}
package visitor;

public class Client {
    public static void main(String[] args) {
        ObjectStructure o = new ObjectStructure();
        o.attach(new ConcreteElementA());
        o.attach(new ConcreteElementB());

        ConcreteVisitor1 visitor1 = new ConcreteVisitor1();
        ConcreteVisitor2 visitor2 = new ConcreteVisitor2();

        o.accept(visitor1);
        o.accept(visitor2);
    }
}

开发中的场景

  • XML文档解析器设计
  • 编译器的设计
  • 复杂集合对象的处理

小结

访问者模式适用于数据结构相对稳定的系统。它把数据结构和作用于结构上的操作之间的耦合解脱开,使得操作集合可以相对自由地演化。

目的

访问者模式的目的是要把处理从数据结构分离出来。很多系统可以按照算法和数据结构分开,如果这样的系统有比较稳定的数据结构,又有易于变化的算法的话,使用访问者模式就是比较合适的,因为访问者模式使得算法操作的增加变得容易。反之,如果这样的系统数据结构相对易于变化,经常要有新的数据对象增加进来,就不适合使用访问者模式。

优点

增加新的操作很容易,因为增加新的操作就意味着增加一个新的访问者。访问者模式将有关的行为集中到一个访问者对象中。

缺点

使增加新的数据结构变得困难。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 定义 访问者模式是对象的行为模式。访问者模式的目的是封装一些施加于某种数据结构元素之上的操作。一旦这些操作需要修改...
    步积阅读 1,243评论 0 3
  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 32,022评论 2 89
  • 1 场景问题# 1.1 扩展客户管理的功能## 考虑这样一个应用:扩展客户管理的功能。 既然是扩展功能,那么肯定是...
    七寸知架构阅读 2,972评论 1 58
  • 有吃有喝有书看 有水有烟有懒散 有未知的姑娘还没有见面 有一场电影她会和我一起看 有阳光和想象的沙漠探险 有摇滚乐...
    关馨仁阅读 157评论 0 1
  • 丙申灼灼洪都府,本是过客遇枝扶。 不往却似滕王客,半世才华半沉沦。
    醍醐道人阅读 211评论 0 2