Java 匿名访客

Java 匿名访客,java,allocation,anonymous,visitor,Java,Allocation,Anonymous,Visitor,听起来很恐怖,嗯 我正在开发一个GUI驱动的应用程序,它大量使用访问者模式。我采用这种方法是因为以特定于类的方式处理大量图形元素对我来说很重要,尽管类本身需要充当简单的数据对象。在这方面,它们完全不知道它们在我的应用程序逻辑中所处的众多场景 这种设计选择的问题是,随着应用程序的增长,我发现自己不得不不断地对访问者实现进行运行时分配,以便在方法体中描述特定于类的代码。由于其中许多依赖于调用时提供的参数,因此我无法将其中许多提取到可重用的静态实现中 下面是一个示例,使用运行时传递的Shaker对象,

听起来很恐怖,嗯

我正在开发一个GUI驱动的应用程序,它大量使用访问者模式。我采用这种方法是因为以特定于类的方式处理大量图形元素对我来说很重要,尽管类本身需要充当简单的数据对象。在这方面,它们完全不知道它们在我的应用程序逻辑中所处的众多场景

这种设计选择的问题是,随着应用程序的增长,我发现自己不得不不断地对访问者实现进行运行时分配,以便在方法体中描述特定于类的代码。由于其中许多依赖于调用时提供的参数,因此我无法将其中许多提取到可重用的
静态
实现中

下面是一个示例,使用运行时传递的
Shaker
对象,仅对
按钮类型执行操作

private abstract class Graphical implements Visitor.Dispatch {
    /* Position. */
    private int X;
    private int Y;
};

private final class Button extends Graphical {
    @Override public final void onVisit(final Visitor pVisitor) { pVisitor.onReceived(this); } };

private final class ScrollBar extends Graphical {
    @Override public final void onVisit(final Visitor pVisitor) { pVisitor.onReceived(this); }
};

public static interface Visitor {
    /* Adapter. */
    public static class Adapter implements Visitor {
        @Override public void onReceived(   Button    pButton) { }
        @Override public void onReceived(ScrollBar pScrollBar) { }
    };
    /* Dispatch Method. */
    public static interface Dispatch {
        public abstract void onVisit(final Visitor pVisitor);
    };
    /* Visitor Implementations. */
    public abstract void onReceived(final    Button    pButton);
    public abstract void onReceived(final ScrollBar pScrollBar);
};

/* Iterates through a List of Graphicals and Shakes a Button. */
public static void onShakeButtons(final List<Graphical> pGraphicals, final Shaker pShaker) {
    /* Allocate a Visitor. */
    final Visitor.Adapter lVisitor = new Visitor.Adapter() { @Override public void onReceived(final Button pButton) {
        /* Shake the Button! */
        pShaker.onShake(pButton);
    } };
    /* Iterate the Graphicals. */
    for(final Graphical lGraphical : pGraphicals) { lGraphical.onVisit(lVisitor); }
}
私有抽象类图形实现Visitor.Dispatch{
/*位置*/
私人INTX;
私营企业;
};
私有最终类按钮扩展了图形{
@重写公共最终无效onVisit(最终访问者pVisitor){pVisitor.onReceived(this);}};
私有最终类滚动条扩展了图形{
@重写公共最终无效onVisit(最终访问者pVisitor){pVisitor.onReceived(this);}
};
公共静态接口访问者{
/*适配器*/
公共静态类适配器实现访问者{
@覆盖公共无效onReceived(按钮pButton){}
@重写接收到的公共void(滚动条pScrollBar){}
};
/*调度方法*/
公共静态接口调度{
公开摘要无效访问(最终访客访客);
};
/*访问者实现*/
收到公共摘要作废(最终按钮pButton);
收到的公共摘要无效(最终滚动条pScrollBar);
};
/*在图形列表中迭代并晃动按钮*/
ShakeButtons上的公共静态无效(最终列表PGraphics、最终Shaker pShaker){
/*分配访客*/
final Visitor.Adapter lVisitor=new Visitor.Adapter(){@Override public void onReceived(final按钮pButton){
/*摇动按钮*/
pShaker.onShake(pButton);
} };
/*迭代图形*/
对于(最终图形:图形){lGraphical.onVisit(lVisitor);}
}
有人能就如何减少我的分配数量提出建议吗?或者我真的对模式应该如何应用有误解吗?

有一个选择。 您可以在访问者中为非静态对象创建容器,并使用新的非静态对象更新这些容器,然后重用访问者

public class ConcreteVisitor extends Visitor {

        private final AtomicReference<MyClass> mValue_1 = new AtomicReference<MyClass>();

        private final AtomicReference<SomeClass> mValue_2 = new AtomicReference<SomeClass>();

        public void updateVisitor(MyClass newMyClass, SomeClass newSomeClass) {
            mValue_1.set(newMyClass)
            mValue_2.set(newSomeClass)
        }

        @Override
        public void visitElement_1(Element_1 element) {
            // use your updated values here
        }

        @Override
        public void visitElement_2(Element_2 element) {
            // use your updated values here
        }
    }

我使用AtomicReference作为容器,但可以使用您的自定义容器类。

您能展示一个具有代表性的代码示例吗?创建对象有什么问题?Java是一个OO对象。创建对象是正常的、预期的和快速的。是否存在一个具体的问题?如果你使用匿名访问者很多,你应该考虑在java 8中使用lambdas,因为这些更简单更干净。@ PeterLawrey访问者只有一个抽象方法才有用。我真的不知道lambdas如何帮助实现访问者接口。@PeterLawrey这是一个关于lambdas的好主意,多个方法的问题通常会得到缓解,因为我经常使用访问者的适配器。不幸的是,我仍然像一些穴居人一样使用Java 7。这是一个不错的想法,尽管我认为它可能有点不灵活(我必须为每个操作分配一些特定的实现,其中有许多类型)。此外,我还担心在很少使用的调用中存在的任何长期引用。
    // You create it only once:
    Visitor concreteVisitor = new ConcreteVisitor();

    // and reuse it all the time
    concreteVisitor.updateVisitor(newMyClass, newSomeClass);
    concreteVisitor.visitElement(element);