java - 匿名访客

标签 java allocation anonymous visitor-pattern

听起来令人毛骨悚然,呵呵...

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

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

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

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); }
}

任何人都可以就如何减少我正在进行的分配数量提出任何建议吗?或者我对如何应用该模式有真正的误解吗?

最佳答案

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

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
        }
    }

当您需要重用访问者时,只需更新值然后再次运行即可:

    // You create it only once:
    Visitor concreteVisitor = new ConcreteVisitor();

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

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

关于java - 匿名访客,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34946418/

相关文章:

java - 表格中的页码

java - 如何限制 JPA Batch 查询的内存使用?

multithreading - 如何在应用程序关闭时终止 Delphi 中的匿名线程?

c# - 如何避免 ref 参数?

C++ 内存分配器架构

java - 为什么会出现 "No suitable method found for anonymous "错误?

JavaFX 警报按钮栏大小调整

java - 预约类

c++ - 我们可以在 C 中返回一个包含数组的结构吗?

c# - 到 "new"或不到 "new"