> For the complete documentation index, see [llms.txt](https://maxwell.gitbook.io/way-to-architect/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maxwell.gitbook.io/way-to-architect/she-ji-mo-shi/hang-wei-xing/fang-wen-zhe-mo-shi.md).

# 访问者模式

**访问者模式（Visitor）：**&#x8868;示一个作用于某对象结构中的各元素的操作。它可以使你在不改变各元素类的前提下，定义作用于这些元素上的新操作。

**使用场景：**&#x31;、对象结构中对象对应的类很少改变，但经常需要在此对象结构上定义新的操作。 2、需要对一个对象结构中的对象进行很多不同的并且不相关的操作，而需要避免让这些操作"污染"这些对象的类，也不希望在增加新操作时修改这些类。

**访问者模式中的角色**

* **Visitor：**&#x8BBF;问者接口，为所有的访问者对象声明一个visit方法，用来代表为对象结构添加的功能，理论上可以代表任意的功能。
* **ConcreteVisitor：**&#x5177;体访问者实现对象，实现要真正被添加到对象结构中的功能。
* **Element：**&#x62BD;象的元素对象，对象结构的顶层接口，定义接受访问的操作。
* **ConcreteElement：**&#x5177;体元素对象，对象结构中具体的对象，也是被访问的对象，通常会回调访问者的真实功能，同时开放自身的数据供访问者使用。
* **ObjectStructure：**&#x901A;常包含多个被访问的对象，它可以遍历这多个被访问的对象，也可以让访问者访问它的元素。可以是一个复合或是一个集合，如一个列表或无序集合。

**访问者模式结构代码**

Visitor：访问者接口

```java
public interface Visitor {

    //访问元素A，相当于给元素A增加功能
    void visitConcreteElementA(ConcreteElementA elementA);

    //访问元素B，相当于给元素B增加功能
    void visitConcreteElementB(ConcreteElementB elementB);

}
```

Element：抽象元素类

```java
public abstract class Element {

    public abstract void accept(Visitor visitor);
}
```

ConcreteElement

```java
//具体元素对象
public class ConcreteElementA extends Element{
    @Override
    public void accept(Visitor visitor) {
        visitor.visitConcreteElementA(this);
    }
}
//具体元素对象
public class ConcreteElementB extends Element{
    @Override
    public void accept(Visitor visitor) {
        visitor.visitConcreteElementB(this);
    }
}
```

ConcreteVisitor

```java
//实现某一功能的具体访问者
public class ConcreteVisitor1 implements Visitor{

    @Override
    public void visitConcreteElementA(ConcreteElementA elementA) {

    }

    @Override
    public void visitConcreteElementB(ConcreteElementB elementB) {

    }
}
//实现某一功能的具体访问者
public class ConcreteVisitor2 implements Visitor{

    @Override
    public void visitConcreteElementA(ConcreteElementA elementA) {

    }

    @Override
    public void visitConcreteElementB(ConcreteElementB elementB) {

    }
}
```

ObjectStructure：元素集合类

```java
public class ObjectStructure {

    private Collection<Element> collection = new ArrayList<>();

    public void addElement(Element e){
        this.collection.add(e);
    }
    //提供给客户端使用
    public void handleRequest(Visitor visitor){
        for (Element e : collection){
            e.accept(visitor);
        }
    }
}
```

Client

```java
public class Client {

    public static void main(String[] args) {

        ObjectStructure os = new ObjectStructure();
        os.addElement(new ConcreteElementA());
        os.addElement(new ConcreteElementB());

        Visitor visitor = new ConcreteVisitor1();
        os.handleRequest(visitor);

    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://maxwell.gitbook.io/way-to-architect/she-ji-mo-shi/hang-wei-xing/fang-wen-zhe-mo-shi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
