java - 具有多次访问实现的访问者模式

标签 java design-patterns

我需要对组合模式进行各种操作。我使用复合模式来实现访问者模式。

界面:

public interface Visitor {
    void visit(DayPart dayPart);
    void visit(DayParting dayParting);
    void visit(TimePart timePart);
}

复合类中的访问者方法:

public void accept(Visitor visitor) {
        visitor.visit(this);
        Iterator<AbstractComposite> itr = iterator();
        Parting children;
        while (itr.hasNext()) {
            children = (Parting) itr.next();
            children.accept(visitor);
        }
    }

叶子上的访问者方法:

@Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }

访问者类实现:

public class myVisitor implements Visitor {
    @Override
    public void visit(DayPart dayPart) {
        //do something (or not)
    }

    @Override
    public void visit(DayParting dayParting){
        // do something (or not)
    }

    @Override
    public void visit(TimePart timePart){
        //do something (or not)
    }
}

问题是我需要对组合进行各种操作,并且通过这种设计,我需要为每次“访问”我需要做的事情创建一个新类,这是非常不切实际的。

所以,我想用内部类做这样的事情(我输入通用名称)。

public abstract class VisitorImpl implements Visitor {
    //Hook methods
    @Override
    public void visit(Composite composite) {
        //hok method
    }

    @Override
    public void visit(Composite2  composite2) {
        //hook method
    }

    @Override
    public void visit(Leaf leaf) {
        //hook method
    }

    public static Visitor ParticularVisitor(){
        return new ParticularVisitor();
    }
    private static class ParticularVisitor extends VisitorImpl {
        @Override
        public void visit(Composite composite) {
            //do something real.
        }
        @Override
        public void visit(Composite2 composite2) {
            //do something real.
        }

        @Override
        public void visit(Leaf leaf) {
            //do something real.
        }
    }
    private static class ParticularVisitor_2 extends VisitorImpl {
        public ParticularVisitor_2(){
        }
        @Override
        public void visit(Leaf leaf){
            //Just do something with this
        }
    }
}

这是解决我的问题的好方法吗?有什么改进吗?

最佳答案

Visitor Design Pattern

Intent

  • Represent an operation to be performed on the elements of an object structure.
  • Visitor lets you define a new operation without changing the classes of the elements on which it operates.
  • The classic technique for recovering lost type information.
  • Do the right thing based on the type of two objects.
  • Double dispatch

Problem

Many distinct and unrelated operations need to be performed on node objects in a heterogeneous aggregate structure. You want to avoid "polluting" the node classes with these operations. And, you don't want to have to query the type of each node and cast the pointer to the correct type before performing the desired operation.

来自:Sourcemaking

所以,你的“问题”

"The problem is that i need do various operations over the Composition and with this design i need to do a new class for every "visit" what i need to do and this is pretty impractical."

不完全是一个问题,问题是 Visitor 应该如何工作。

One operation to be performed = one visitor

如果您的操作之间没有关系,那就是您可以使用 Visitor 做的最好的事情。

关于java - 具有多次访问实现的访问者模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37688355/

相关文章:

java - Spring 中的 AutoWired Setters 而不是 AutoWired 实例变量

c# - 在一个应用程序中使用多个数据库

html - 是否有工具可以将任何网站转换成其框架正在使用的模板?

从客户端发送到服务器的 Java 消息

java - 我可以在我的 Java 程序中使用 Robot Framework 吗?

java - Java 循环双向链表 - 说明

java - 如何访问和显示(打印)存储在从 Java 中的 asList View 对象创建的子 ListView 中的对象

java - 如何使用 Singleton 类来获得多个连接?

c++ - 继承包含另一个派生类的 C++ 类

java - 当 Web 应用程序位于 webapps 的子目录中时,Jetty 不会路由到 servlet