Skip to main content
 首页 » 编程设计

Java访问者模式

2022年07月19日142wuhuacong

介绍Java访问者模式

本文我们介绍一个行为设计模式————Java访问者模式。用于在不更改任何现有代码的情况下向现有的层次结构类添加新的行为。

1. 概述

使用现实世界类比有助于我们理解设计模式。购物车是比较好理解的例子,购物车中有一组商品,当需要结账时收银员角色为访问者,依次获取不同的商品信息,如价格、重量等,为了计算总价。

java内置包中有不少示例:

  • javax.lang.model.element.AnnotationValue and AnnotationValueVisitor
  • javax.lang.model.element.Element and ElementVisitor
  • javax.lang.model.type.TypeMirror and TypeVisitor
  • java.nio.file.FileVisitor and SimpleFileVisitor
  • javax.faces.component.visit.VisitContext and VisitCallback

其uml描述图示如下:

uml

下面通过示例代码进行描述,希望让你更好理解。

2. 导出形状元素至xml

该示例需要导出一组几何形状值xml中。关键是我们不希望直接更改形状类代码,或者至少最少量改动。最终访问者模式构建的架构支持增强形状层次类的行为,但不改变这些类的代码。

2.1. 元素接口及实现类

首先定义形状接口,其他所有几何元素都实现该接口:

public interface Shape { 
    void move(int x, int y); 
    void draw(); 
    String accept(Visitor visitor); 
} 

下面是几个几何图形实现类,首先是dot:

@Setter 
@Getter 
public class Dot implements Shape { 
    private int id; 
    private int x; 
    private int y; 
 
    public Dot() { 
    } 
 
    public Dot(int id, int x, int y) { 
        this.id = id; 
        this.x = x; 
        this.y = y; 
    } 
 
    @Override 
    public void move(int x, int y) { 
        // move shape 
    } 
 
    @Override 
    public void draw() { 
        // draw shape 
    } 
 
    public String accept(Visitor visitor) { 
        return visitor.visitDot(this); 
    } 
} 

Circle类定义:

public class Circle extends Dot { 
    private int radius; 
 
    public Circle(int id, int x, int y, int radius) { 
        super(id, x, y); 
        this.radius = radius; 
    } 
 
    @Override 
    public String accept(Visitor visitor) { 
        return visitor.visitCircle(this); 
    } 
 
    public int getRadius() { 
        return radius; 
    } 
} 

Rectangle类定义:

@Setter 
@Getter 
public class Rectangle implements Shape { 
    private int id; 
    private int x; 
    private int y; 
    private int width; 
    private int height; 
 
    public Rectangle(int id, int x, int y, int width, int height) { 
        this.id = id; 
        this.x = x; 
        this.y = y; 
        this.width = width; 
        this.height = height; 
    } 
 
    @Override 
    public String accept(Visitor visitor) { 
        return visitor.visitRectangle(this); 
    } 
 
    @Override 
    public void move(int x, int y) { 
        // move shape 
    } 
 
    @Override 
    public void draw() { 
        // draw shape 
    } 
} 

组合形状类CompoundShape定义:

public class CompoundShape implements Shape { 
    public int id; 
    public List<Shape> children = new ArrayList<>(); 
 
    public CompoundShape(int id) { 
        this.id = id; 
    } 
 
    @Override 
    public void move(int x, int y) { 
        // move shape 
    } 
 
    @Override 
    public void draw() { 
        // draw shape 
    } 
 
    public int getId() { 
        return id; 
    } 
 
    @Override 
    public String accept(Visitor visitor) { 
        return visitor.visitCompoundGraphic(this); 
    } 
 
    public void add(Shape shape) { 
        children.add(shape); 
    } 
} 

2.2. 访问者接口及实现类

首先定义访问者接口:

public interface Visitor { 
    String visitDot(Dot dot); 
 
    String visitCircle(Circle circle); 
 
    String visitRectangle(Rectangle rectangle); 
 
    String visitCompoundGraphic(CompoundShape cg); 
} 

下面定义XMLExportVisitor实现类:

public class XMLExportVisitor implements Visitor { 
 
    public String export(Shape... args) { 
        StringBuilder sb = new StringBuilder(); 
        for (Shape shape : args) { 
            sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "\n"); 
            sb.append(shape.accept(this)).append("\n"); 
            System.out.println(sb.toString()); 
            sb.setLength(0); 
        } 
        return sb.toString(); 
    } 
 
    public String visitDot(Dot d) { 
        return "<dot>" + "\n" + 
                "    <id>" + d.getId() + "</id>" + "\n" + 
                "    <x>" + d.getX() + "</x>" + "\n" + 
                "    <y>" + d.getY() + "</y>" + "\n" + 
                "</dot>"; 
    } 
 
    public String visitCircle(Circle c) { 
        return "<circle>" + "\n" + 
                "    <id>" + c.getId() + "</id>" + "\n" + 
                "    <x>" + c.getX() + "</x>" + "\n" + 
                "    <y>" + c.getY() + "</y>" + "\n" + 
                "    <radius>" + c.getRadius() + "</radius>" + "\n" + 
                "</circle>"; 
    } 
 
    public String visitRectangle(Rectangle r) { 
        return "<rectangle>" + "\n" + 
                "    <id>" + r.getId() + "</id>" + "\n" + 
                "    <x>" + r.getX() + "</x>" + "\n" + 
                "    <y>" + r.getY() + "</y>" + "\n" + 
                "    <width>" + r.getWidth() + "</width>" + "\n" + 
                "    <height>" + r.getHeight() + "</height>" + "\n" + 
                "</rectangle>"; 
    } 
 
    public String visitCompoundGraphic(CompoundShape cg) { 
        return "<compound_graphic>" + "\n" + 
                "   <id>" + cg.getId() + "</id>" + "\n" + 
                _visitCompoundGraphic(cg) + 
                "</compound_graphic>"; 
    } 
 
    private String _visitCompoundGraphic(CompoundShape cg) { 
        StringBuilder sb = new StringBuilder(); 
        for (Shape shape : cg.children) { 
            String obj = shape.accept(this); 
            // Proper indentation for sub-objects. 
            obj = "    " + obj.replace("\n", "\n    ") + "\n"; 
            sb.append(obj); 
        } 
        return sb.toString(); 
    } 
 
} 

2.3. 实现客户端进行测试

public class Demo { 
    public static void main(String[] args) { 
        Dot dot = new Dot(1, 10, 55); 
        Circle circle = new Circle(2, 23, 15, 10); 
        Rectangle rectangle = new Rectangle(3, 10, 17, 20, 30); 
 
        CompoundShape compoundShape = new CompoundShape(4); 
        compoundShape.add(dot); 
        compoundShape.add(circle); 
        compoundShape.add(rectangle); 
 
        CompoundShape c = new CompoundShape(5); 
        c.add(dot); 
        compoundShape.add(c); 
 
        export(circle, compoundShape); 
    } 
 
    private static void export(Shape... shapes) { 
        XMLExportVisitor exportVisitor = new XMLExportVisitor(); 
        System.out.println(exportVisitor.export(shapes)); 
    } 
} 

输出结果:

<?xml version="1.0" encoding="utf-8"?> 
<circle> 
    <id>2</id> 
    <x>23</x> 
    <y>15</y> 
    <radius>10</radius> 
</circle> 
 
<?xml version="1.0" encoding="utf-8"?> 
<compound_graphic> 
   <id>4</id> 
    <dot> 
        <id>1</id> 
        <x>10</x> 
        <y>55</y> 
    </dot> 
    <circle> 
        <id>2</id> 
        <x>23</x> 
        <y>15</y> 
        <radius>10</radius> 
    </circle> 
    <rectangle> 
        <id>3</id> 
        <x>10</x> 
        <y>17</y> 
        <width>20</width> 
        <height>30</height> 
    </rectangle> 
    <compound_graphic> 
       <id>5</id> 
        <dot> 
            <id>1</id> 
            <x>10</x> 
            <y>55</y> 
        </dot> 
    </compound_graphic> 
</compound_graphic> 

3. 总结

本文通过示例介绍了访问者模式,下面总结下其优缺点。

优点:

  • 如果业务有变化,仅需要改变访问者实现类,无需修改各个元素。
  • 增加新的元素很容易,仅需要修改访问者接口和实现类,已存在的各个元素不受影响。

缺点:

  • 设计时需先知道visit()方法的返回值类型,否则必须改变接口及所有实现类。
  • 如果访问者接口的实现太多比较难以扩展。

本文参考链接:https://blog.csdn.net/neweastsun/article/details/102649413
阅读延展