visitor

参考:http://en.wikipedia.org/wiki/Visitor_pattern

class=thumbimage

Example
The following example is in the Java programming language, and shows how the contents of a tree of nodes (in this case describing the components of a car) can be printed. Instead of creating "print" methods for each subclass (Wheel, Engine, Body, and Car), a single class (CarElementPrintVisitor) performs the required printing action. Because different subclasses require slightly different actions to print properly, CarElementDoVisitor dispatches actions based on the class of the argument passed to it.

alt=A Diagram of the Java Code Example.        I, the copyright holder of this work, hereby release it into the public domain.  This applies worldwide. In case this is not legally possible, I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.

interface CarElementVisitor {
    void visit(Wheel wheel);
    void visit(Engine engine);
    void visit(Body body);
    void visit(Car car);
}
interface CarElement {
    void accept(CarElementVisitor visitor); // CarElements have to provide accept().
}
class Wheel implements CarElement {
    private String name;
    public Wheel(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
    public void accept(CarElementVisitor visitor) {
        visitor.visit(this);
    }
}
class Engine implements CarElement {
    public void accept(CarElementVisitor visitor) {
        visitor.visit(this);
    }
}
class Body implements CarElement {
    public void accept(CarElementVisitor visitor) {
        visitor.visit(this);
    }
}
class Car implements CarElement{
    CarElement[] elements;
    public CarElement[] getElements() {
        return elements.clone(); // Return a copy of the array of references.
    }
    public Car() {
        this.elements = new CarElement[]
          { new Wheel("front left"), new Wheel("front right"),
            new Wheel("back left") , new Wheel("back right"),
            new Body(), new Engine() };
    }
    public void accept(CarElementVisitor visitor) { 
        for(CarElement element : this.getElements()) {
            element.accept(visitor);
        }
        visitor.visit(this); 
    }
}
class CarElementPrintVisitor implements CarElementVisitor {
    public void visit(Wheel wheel) {     
        System.out.println("Visiting "+ wheel.getName()
                            + " wheel");
    }
    public void visit(Engine engine) {
        System.out.println("Visiting engine");
    }
    public void visit(Body body) {
        System.out.println("Visiting body");
    }
    public void visit(Car car) {     
        System.out.println("Visiting car");
    }
}
class CarElementDoVisitor implements CarElementVisitor {
    public void visit(Wheel wheel) {
        System.out.println("Kicking my "+ wheel.getName() + " wheel");
    }
    public void visit(Engine engine) {
        System.out.println("Starting my engine");
    }
    public void visit(Body body) {
        System.out.println("Moving my body");
    }
    public void visit(Car car) {
        System.out.println("Starting my car");
    }
}
public class VisitorDemo {
    static public void main(String[] args){
        Car car = new Car();
        car.accept(new CarElementPrintVisitor());
        car.accept(new CarElementDoVisitor());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设计模式中的访问者模式(Visitor Pattern)是一种行为型模式,它允许你在不修改对象结构的情况下定义新的操作。访问者模式通过将操作封装在访问者对象中,使得可以在不改变被访问对象的类的前提下,定义新的操作。 访问者模式的参与者包括: - 抽象接口(Glaph):定义了需要操纵对象的抽象接口。 - 具体实现类(Character):实现了抽象接口,并接受访问者的访问和操作。 - 访问者抽象类(Visitor):定义了访问者的缺省方法。 - 具体访问者(ConcreteVisitor):实现了访问者抽象类,对需要增加的操作进行实现。 Java中的实际应用举例可以以ASM技术为例。ASM是一个Java字节码操纵框架,它可以用来动态生成、修改和分析Java字节码。在ASM中,访问者模式被广泛应用于字节码的访问和操作。 以下是一个使用访问者模式的Java代码示例: ```java // 抽象接口 public interface Acceptable { void accept(Visitor visitor); } // 具体实现类 public class Character implements Acceptable { @Override public void accept(Visitor visitor) { visitor.visit(this); } } // 访问者抽象类 public abstract class Visitor { public abstract void visit(Character character); } // 具体访问者 public class ConcreteVisitor extends Visitor { @Override public void visit(Character character) { // 对Character进行操作 } } // 使用访问者模式 public class Main { public static void main(String[] args) { Character character = new Character(); Visitor visitor = new ConcreteVisitor(); character.accept(visitor); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值