访问者模式 logo

访问者模式

Organization
microwind
访问者模式

在不改变对象结构的前提下,为对象添加新的操作。

Overview

Publishermicrowind
Repositoryai-skills
Skill name访问者模式
Stars
93
Forks
25
Bundled files
3
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 3 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by microwind on GitHub. Read the source before you install it.

Installation

Install the 访问者模式 AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/microwind/ai-skills/tree/main/design-patterns/visitor-pattern
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/microwind/ai-skills.git /tmp/ai-skills
mkdir -p .claude/skills
cp -r /tmp/ai-skills/design-patterns/visitor-pattern .claude/skills/microwind-34
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable 访问者模式 in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads 访问者模式 on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so 访问者模式 is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

访问者模式 (Visitor Pattern)

核心概念

Visitor是行为型设计模式,使用双分派实现在不改变对象结构的情况下增加新操作。分离操作与对象结构,使得新增操作无需修改元素类。

核心原则: 双分派、操作与数据分离、开放封闭原则


何时使用

始终使用:

  • 需要为对象结构中的元素进行多种不同操作
  • 对象结构较稳定但操作经常变化
  • 需要避免修改元素类
  • 支持多种不同的元素类型

触发短语: "为不同对象类型执行不同操作?" "如何为现有对象添加操作?"

典型场景:

场景元素操作用途
编译器AST节点类型检查/代码生成语法分析
报告数据元素不同格式输出数据导出
遍历树/图节点访问/统计图处理

4个实现方法

方法1: 经典访问者模式

java
// 元素接口
public interface Element {
    void accept(Visitor visitor);
}

// 具体元素
public class ConcreteElementA implements Element {
    @Override
    public void accept(Visitor v) { v.visitA(this); }
}

public class ConcreteElementB implements Element {
    @Override
    public void accept(Visitor v) { v.visitB(this); }
}

// 访问者接口
public interface Visitor {
    void visitA(ConcreteElementA e);
    void visitB(ConcreteElementB e);
}

// 具体访问者
public class ConcreteVisitor implements Visitor {
    @Override
    public void visitA(ConcreteElementA e) {
        System.out.println("Process A");
    }
    
    @Override
    public void visitB(ConcreteElementB e) {
        System.out.println("Process B");
    }
}

方法2: 参数化访问器

使用参数化类型支持返回值。

java
public interface Visitor<T> {
    T visitA(ElementA e);
    T visitB(ElementB e);
}

public class SumVisitor implements Visitor<Integer> {
    @Override
    public Integer visitA(ElementA e) { return e.getValue(); }
    
    @Override
    public Integer visitB(ElementB e) { return e.getValue() * 2; }
}

// 使用
Visitor<Integer> v = new SumVisitor();
int result = elements.stream()
    .map(e -> e.accept(v))
    .reduce(0, Integer::sum);

方法3: 链式访问器

支持多个访问者链式执行。

java
public class ChainedVisitor implements Visitor {
    private List<Visitor> chain = new ArrayList<>();
    
   public ChainedVisitor add(Visitor v) { 
        chain.add(v); 
        return this; 
    }
    
    @Override
    public void visitA(ElementA e) {
        for (Visitor v : chain) v.visitA(e);
    }
}

// 使用
new ChainedVisitor()
    .add(new ValidateVisitor())
    .add(new TransformVisitor())
    .add(new PrintVisitor());

方法4: 函数式访问器

用Lambda替代Visitor类。

java
public class FunctionalVisitor {
    private Map<Class<?>, Consumer<?>> handlers = new HashMap<>();
    
    @SuppressWarnings("unchecked")
    public <T> FunctionalVisitor handle(Class<T> type, Consumer<T> handler) {
        handlers.put(type, handler);
        return this;
    }
    
    public void visit(Element e) {
        Consumer handler = handlers.get(e.getClass());
        if (handler != null) handler.accept(e);
    }
}

// 使用
new FunctionalVisitor()
    .handle(ElementA.class, e -> System.out.println("A"))
    .handle(ElementB.class, e -> System.out.println("B"))
    .visit(elementA);

常见问题

❌ 问题1: 元素结构变更

症状: 添加新元素类型导致所有访问者需改

解决: 提供默认处理

java
public interface Visitor {
    void visitA(ElementA e);
    void visitB(ElementB e);
    
    // 默认处理未知元素
    default void visitUnknown(Element e) {}
}

❌ 问题2: 访问器数爆增

症状: 每个操作需要一个访问者类

解决: 使用函数式或配置驱动

java
// 配置驱动
Map<String, Consumer> visitors = new HashMap<>();
visitors.put("print", e -> System.out.println(e));
visitors.put("count", e -> count++);

❌ 问题3: 循环引用

症状: 图结构中可能无限循环

解决:

java
public class CyclicVisitor implements Visitor {
    private Set<Element> visited = new HashSet<>();
    
    @Override
    public void visitA(ElementA e) {
        if (visited.contains(e)) return;
        visited.add(e);
        // 处理...
    }
}

❌ 问题4: 性能开销

症状: 大对象结构访问缓慢

解决: 缓存和并行处理

java
public class OptimizedVisitor implements Visitor {
    private Map<Element, Object> cache = new ConcurrentHashMap<>();
    
    private Object getOrCompute(Element e, Function<Element, Object> comp) {
        return cache.computeIfAbsent(e, comp);
    }
}

最佳实践

✅ 分离操作与结构 - 访问者集中操作逻辑 ✅ 支持扩展 - 易于添加新访问者 ✅ 处理循环 - 记录已访问节点 ✅ 性能优化 - 缓存计算结果 ✅ 清晰命名 - visit方法名清楚 ✅ 提供默认 - 简化新访问者开发


真实案例

  • 编译器: AST的类型检查、代码生成
  • 报告系统: 生成多种格式输出
  • 遍历: 树/图的搜索、统计

深度理论:双分派原理

单分派 vs 双分派

单分派(传统OOP)
java
// 单分派:执行方法仅基于接收者类型
public void process(Element e) {
    if (e instanceof ElementA) {
        processA((ElementA) e);
    } else if (e instanceof ElementB) {
        processB((ElementB) e);
    }
}
// 问题:客户端需要instanceof检查
双分派(访问者模式)
java
// 双分派:基于接收者 + 参数的类型
public interface Element {
    void accept(Visitor v); // 第一次分派:方法选择基于Element类型
}

public class ElementA implements Element {
    @Override
    public void accept(Visitor v) {
        v.visitA(this); // 第二次分派:方法选择基于Visitor类型
    }
}

// 客户端无需类型检查
element.accept(visitor);

双分派如何工作

step1: element.accept(visitor)
       |
       +-- ElementA重写accept()
       |   |
       +-- 调用 visitor.visitA(this)
           |
           +-- PrintVisitor重写visitA()
               +-- 执行对应逻辑

单/双分派对比

特性单分派双分派
类型检查客户端隐式
代码简洁低高
变更影响客户端更改无需改客户端
新元素需改客户端只需新的accept
新操作新访问者只需新访问者

访问者模式 vs 其他模式

访问者 vs 策略

维度访问者策略
关注点操作算法
结构多元素单对象
分派双分派单分派
应用场景异构集合同构对象

访问者 vs 命令

java
// 访问者:固定接收者,遍历所有元素
visitor.visit(element);

// 命令:固定元素集,执行不同操作
command.execute();

五种高级实现

1. 基于反射的通用访问者

何时用:元素类型动态,无需修改访问者

java
public class ReflectionVisitor {
    public void visit(Object element) {
        Method visitMethod = findVisitMethod(element.getClass());
        if (visitMethod != null) {
            visitMethod.invoke(this, element);
        }
    }
    
    private Method findVisitMethod(Class<?> elementClass) {
        String methodName = "visit" + elementClass.getSimpleName();
        try {
            return this.getClass().getDeclaredMethod(methodName, elementClass);
        } catch (NoSuchMethodException e) {
            return null;
        }
    }
    
    // 实现具体visit方法
    public void visitElementA(ElementA e) { /* handle A */ }
    public void visitElementB(ElementB e) { /* handle B */ }
}

2. 动态函数映射访问者

何时用:操作灵活,支持运行时配置

java
public class DynamicVisitor<T> {
    private Map<Class<?>, Function<?, T>> handlers = new HashMap<>();
    
    @SuppressWarnings("unchecked")
    public <E> void register(Class<E> type, Function<E, T> handler) {
        handlers.put(type, handler);
    }
    
    @SuppressWarnings("unchecked")
    public T visit(Object element) {
        Function handler = handlers.get(element.getClass());
        if (handler == null) {
            throw new UnsupportedOperationException(
                "No handler for " + element.getClass());
        }
        return (T) handler.apply(element);
    }
}

// 使用
DynamicVisitor<Integer> calculator = new DynamicVisitor<>();
calculator.register(NumberNode.class, n -> n.value);
calculator.register(BinaryOp.class, b -> calculate(b));

int result = ast.accept(calculator);

3. 分组访问者(解决访问者爆炸)

何时用:很多访问者,需分类管理

java
public class GroupedVisitor {
    private List<Visitor> validators = new ArrayList<>();
    private List<Visitor> transformers = new ArrayList<>();
    private List<Visitor> outputters = new ArrayList<>();
    
    public void validate(Element e) {
        validators.forEach(v -> e.accept(v));
    }
    
    public void transform(Element e) {
        transformers.forEach(v -> e.accept(v));
    }
    
    public void output(Element e) {
        outputters.forEach(v -> e.accept(v));
    }
}

4. 上下文感知访问者

何时用:访问过程需要上下文信息

java
public interface Element {
    void accept(ContextualVisitor visitor, Context ctx);
}

public interface ContextualVisitor {
    void visitA(ElementA e, Context ctx);
    void visitB(ElementB e, Context ctx);
}

public class Context {
    private int depth = 0;
    private Set<Element> visited = new HashSet<>();
    
    public void enter() { depth++; }
    public void exit() { depth--; }
    public boolean hasVisited(Element e) { return visited.contains(e); }
    public void mark(Element e) { visited.add(e); }
}

public class PrintingVisitor implements ContextualVisitor {
    @Override
    public void visitA(ElementA e, Context ctx) {
        System.out.println("  ".repeat(ctx.depth) + e);
    }
}

5. 异步访问者

何时用:访问操作需要异步执行

java
public interface AsyncElement {
    CompletableFuture<?> accept(AsyncVisitor visitor);
}

public interface AsyncVisitor {
    CompletableFuture<?> visitA(ElementA e);
    CompletableFuture<?> visitB(ElementB e);
}

public class AsyncPrintingVisitor implements AsyncVisitor {
    @Override
    public CompletableFuture<?> visitA(ElementA e) {
        return CompletableFuture.runAsync(() -> System.out.println(e));
    }
}

// 使用
CompletableFuture<?> future = element.accept(visitor);
future.join(); // 等待完成

处理循环引用

问题:图中的循环

java
// ❌ 无限循环
Node a = new Node("A");
Node b = new Node("B");
a.addChild(b);
b.addChild(a); // 循环!

visitor.visit(a); // 无限递归

解决方案1:Set追踪

java
public class CycleDetectingVisitor implements Visitor {
    private Set<Element> visited = new HashSet<>();
    
    @Override
    public void visitA(ElementA e) {
        if (visited.contains(e)) return; // 已访问,跳过
        visited.add(e);
        
        // 处理...
        if (e instanceof Composite) {
            for (Element child : ((Composite) e).getChildren()) {
                child.accept(this);
            }
        }
    }
}

解决方案2:深度限制

java
public class DepthLimitedVisitor implements Visitor {
    private static final int MAX_DEPTH = 100;
    private int currentDepth = 0;
    
    @Override
    public void visitA(ElementA e) {
        if (currentDepth >= MAX_DEPTH) return;
        
        currentDepth++;
        try {
            // 处理...
        } finally {
            currentDepth--;
        }
    }
}

解决方案3:时间戳标记

java
public class TimestampVisitor implements Visitor {
    private static int timestamp = 0;
    private final int visitorId = timestamp++;
    private Map<Element, Integer> visitTime = new HashMap<>();
    
    @Override
    public void visitA(ElementA e) {
        Integer lastTime = visitTime.get(e);
        if (lastTime != null && lastTime == visitorId) {
            return; // 同一访问中已处理
        }
        visitTime.put(e, visitorId);
        // 处理...
    }
}

访问者爆炸问题(N×M问题)

问题描述

  • N种元素类型
  • M种访问操作
  • 需实现 N×M 个方法

例如:10种元素 × 10种操作 = 100个方法

解决方案1:反射+注解

java
public class AnnotationBasedVisitor {
    public void visit(Element e) throws IllegalAccessException {
        Method[] methods = this.getClass().getDeclaredMethods();
        Class<?> elementClass = e.getClass();
        
        for (Method m : methods) {
            VisitHandler handler = m.getAnnotation(VisitHandler.class);
            if (handler != null && m.getParameterCount() == 1) {
                Class<?> paramType = m.getParameterTypes()[0];
                if (paramType.isAssignableFrom(elementClass)) {
                    m.invoke(this, e);
                    return;
                }
            }
        }
    }
}

@VisitHandler
public void handleNumberNode(NumberNode n) { }

@VisitHandler
public void handleBinaryOp(BinaryOp op) { }

解决方案2:Map配置

java
public class MapBasedVisitor {
    private Map<Class<?>, Consumer<?>> operations = new HashMap<>();
    
    {
        operations.put(ElementA.class, e -> handleA((ElementA) e));
        operations.put(ElementB.class, e -> handleB((ElementB) e));
    }
    
    @SuppressWarnings("unchecked")
    public void visit(Element e) {
        Consumer operation = operations.get(e.getClass());
        if (operation != null) {
            operation.accept(e);
        }
    }
    
    private void handleA(ElementA e) { }
    private void handleB(ElementB e) { }
}

解决方案3:分层架构

java
// 基础操作
public interface BaseVisitor {
    void visit(Element e);
}

// 特定域操作
public interface ValidationVisitor extends BaseVisitor { }
public interface TransformationVisitor extends BaseVisitor { }
public interface OutputVisitor extends BaseVisitor { }

// 默认实现共用
public abstract class AbstractVisitor implements BaseVisitor {
    protected void visitDefault(Element e) { }
    
    @Override
    public void visit(Element e) {
        if (e instanceof SpecialElement) {
            visitSpecial((SpecialElement) e);
        } else {
            visitDefault(e);
        }
    }
}

性能优化

1. 缓存策略

java
public class CachingVisitor implements Visitor {
    private Map<Element, Object> cache = new ConcurrentHashMap<>();
    
    @Override
    public void visitA(ElementA e) {
        Object result = cache.computeIfAbsent(e, key -> compute(e));
        use(result);
    }
    
    private Object compute(Element e) {
        // 昂贵计算
        return null;
    }
}

2. 批处理

java
public class BatchVisitor implements Visitor {
    private List<Element> batch = new ArrayList<>();
    private static final int BATCH_SIZE = 100;
    
    @Override
    public void visitA(ElementA e) {
        batch.add(e);
        if (batch.size() >= BATCH_SIZE) {
            processBatch();
            batch.clear();
        }
    }
    
    private void processBatch() {
        // 批量处理比单个处理快
    }
}

3. 并行处理

java
public class ParallelVisitor implements Visitor {
    private ForkJoinPool pool = ForkJoinPool.commonPool();
    
    @Override
    public void visitComposite(CompositeElement e) {
        List<CompletableFuture<?>> tasks = e.getChildren().stream()
            .map(child -> CompletableFuture.runAsync(
                () -> child.accept(this), pool))
            .collect(Collectors.toList());
        
        CompletableFuture.allOf(tasks.toArray(new CompletableFuture[0])).join();
    }
}

测试策略

单元测试

java
public class VisitorTest {
    static class MockElement implements Element {
        @Override
        public void accept(Visitor v) { v.visitMock(this); }
    }
    
    static class TestVisitor implements Visitor {
        List<String> visited = new ArrayList<>();
        
        @Override
        public void visitMock(MockElement e) {
            visited.add("mock");
        }
    }
    
    @Test
    public void testVisitor() {
        Element e = new MockElement();
        TestVisitor v = new TestVisitor();
        
        e.accept(v);
        
        assertTrue(v.visited.contains("mock"));
    }
}

集成测试

java
@Test
public void testComplexStructure() {
    CompositeElement root = buildTree();
    StatisticsVisitor stats = new StatisticsVisitor();
    
    root.accept(stats);
    
    assertEquals(100, stats.getNodeCount());
    assertEquals(50, stats.getMaxDepth());
}

最常见的实现错误

错误1:忘记处理新元素类型

❌ 问题:

java
public void visitA(ElementA e) { }
public void visitB(ElementB e) { }
// 新增ElementC,编译器不报错,运行时异常

✅ 修正:

java
public void visitA(ElementA e) { }
public void visitB(ElementB e) { }
public void visitC(ElementC e) { } // 必须添加

// 或使用默认处理
default void visitUnknown(Element e) { }

错误2:修改访问过程中的结构

❌ 问题:

java
public void visitA(ElementA e) {
    e.getParent().removeChild(e); // 错!正在遍历
}

✅ 修正:

java
public void visitA(ElementA e) {
    toRemove.add(e); // 延迟操作
}

// 遍历结束后清理
void cleanup() {
    toRemove.forEach(e -> e.getParent().removeChild(e));
}

错误3:无限循环

❌ 问题:

java
public void visitA(ElementA e) {
    for (Element child : e.getChildren()) {
        child.accept(this); // 可能循环
    }
}

✅ 修正:

java
private Set<Element> visited = new HashSet<>();

public void visitA(ElementA e) {
    if (visited.contains(e)) return;
    visited.add(e);
    
    for (Element child : e.getChildren()) {
        child.accept(this);
    }
}

决策清单

何时使用访问者模式?

  • 对象结构复杂且稳定
  • 需要对结构执行多种不同操作
  • 新增操作频繁
  • 修改元素类困难(如外部库)

✅ 满足所有条件 → 使用访问者

不适用情况:

  • 结构经常变更 ❌
  • 仅有一两个操作 ❌
  • 类型安全很关键且类型复杂 ❌

与其他模式组合

访问者+组合

java
public abstract class Node {
    abstract void accept(Visitor visitor);
}

public class Leaf extends Node {
    @Override
    void accept(Visitor visitor) { visitor.visitLeaf(this); }
}

public class Composite extends Node {
    List<Node> children;
    
    @Override
    void accept(Visitor visitor) {
        visitor.visitComposite(this);
        children.forEach(c -> c.accept(visitor));
    }
}

访问者+迭代器

java
public interface TraversableElement {
    void accept(Visitor v);
    Iterator<Element> iterator();
}

访问者+工厂

java
public class VisitorFactory {
    public static Visitor createPrinter() { return new PrintVisitor(); }
    public static Visitor createValidator() { return new ValidateVisitor(); }
}

总结对比表

特性经典参数化链式函数式异步
复杂度中低中低高
灵活性高高高低中
性能高高中中低
类型检查✅✅✅❌✅
适用场景复杂操作有返回值多操作链简单操作异步IO

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the 访问者模式 AI skill do?

在不改变对象结构的前提下,为对象添加新的操作。

Why use 访问者模式 on TypingMind?

Because you install it once and use it with any model. 访问者模式 is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install 访问者模式 in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/microwind/ai-skills/tree/main/design-patterns/visitor-pattern. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use 访问者模式?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with 访问者模式?

As many as you like. As long as a model supports skills, you can use 访问者模式 with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the 访问者模式 AI skill free?

Yes. It is published on GitHub by microwind under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇