使用访客模式时的匿名或真实类定义? [英] Anonymous or real class definition when using visitor pattern?

查看:170
本文介绍了使用访客模式时的匿名或真实类定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用访问者模式,您需要在访问者方法中获取变量时,如何继续?

When you use the Visitor pattern and you need to get a variable inside visitor method, how to you proceed ?

我看到两种方法。第一个使用匿名类:

I see two approaches. The first one uses anonymous class :

// need a wrapper to get the result (which is just a String)
final StringBuild result = new StringBuilder();
final String concat = "Hello ";

myObject.accept(new MyVisitor() {

    @Override
    public void visit(ClassA o)
    {
        // this concatenation is expected here because I've simplified the example
        // normally, the concat var is a complex object (like hashtable) 
        // used to create the result variable 
        // (I know that concatenation using StringBuilder is ugly, but this is an example !)
        result.append(concat + "A");
    }

    @Override
    public void visit(ClassB o)
    {
        result.append(concat + "B");
    }
});

System.out.println(result.toString());

缺点:


  • 优点:您不需要为此小行为创建类文件

  • 缺点:在这种情况下,我不喜欢final关键字:匿名类的可读性较差,因为它调用外部变量,您需要使用包装器来获取请求的值(因为关键字final,你不能重新分配变量)

另一种做法是做一个外部访问者类:

Another way to do it is to do an external visitor class :

public class MyVisitor
{
    private String result;
    private String concat;

    public MyVisitor(String concat)
    {
        this.concat = concat;
    }

    @Override
    public void visit(ClassA o)
    {
        result = concat + "A";
    }

    @Override
    public void visit(ClassB o)
    {
        result = concat + "B";
    }

    public String getResult()
    {
        return result;
    }
}

MyVisitor visitor = new MyVisitor("Hello ");
myObject.accept(visitor);
System.out.println(visitor.getResult());

缺点:


  • 优点:所有变量都定义在一个干净的范围内,您不需要一个封装来封装请求的变量

  • 缺点:需要一个外部文件,getResult()方法必须在accept方法之后调用,这是相当难看的,因为你需要知道函数调用顺序来正确使用访问者

你在这种情况下你的做法是什么?首选方法?另一个想法?

You, what's your approach in this case ? Preferred method ? another idea ?

推荐答案

嗯,这两种方法都是有效的和imo,它的确取决于你是否要重用代码不。顺便说一句,你最后一个'Con'点是不完全有效的,因为你不需要一个'外部文件'来声明一个类。这可能是一个内在的类...

Well, both approaches are valid and imo, it really depends on whether you would like to reuse the code or not. By the way, your last 'Con' point is not totally valid since you do not need an 'external file' to declare a class. It might very well be an inner class...

说,我使用访问者的方式是这样的:

That said, the way I use Visitors is like this:

public interface IVisitor<T extends Object> {
    public T visit(ClassA element) throws VisitorException;
    public T visit(ClassB element) throws VisitorException;
}

public interface IVisitable {
    public <T extends Object> T accept(final IVisitor<T> visitor) throws VisitorException;
}

public class MyVisitor implements IVisitor<String> {
    private String concat;

    public MyVisitor(String concat) {
        this.concat = concat;
    }

    public String visit(ClassA classA) throws VisitorException {
        return this.concat + "A";
    }

    public String visit(ClassB classB) throws VisitorException {
        return this.concat + "B";
    }
}

public class ClassA implements IVisitable {
    public <T> T accept(final IVisitor<T> visitor) throws VisitorException {
        return visitor.visit(this);
    }
}

public class ClassB implements IVisitable {
    public <T> T accept(final IVisitor<T> visitor) throws VisitorException {
        return visitor.visit(this);
    }
}

// no return value needed?
public class MyOtherVisitor implements IVisitor<Void> {
    public Void visit(ClassA classA) throws VisitorException {
        return null;
    }

    public Void visit(ClassB classB) throws VisitorException {
        return null;
    }
}

这样,被访问的对象就不了解什么访客想要与他们做,但他们确实返回任何访客想要返回。您的访问者甚至可以通过抛出异常来失败。

That way, the visited objects are ignorant of what the visitor wants to do with them, yet they do return whatever the visitor wants to return. Your visitor can even 'fail' by throwing an exception.

我写了几年前的第一个版本,到目前为止,它在每一种情况下都适用于我。

I wrote the first version of this a few years ago and so far, it has worked for me in every case.

免责声明:我一直是黑客入侵,质量(甚至是编译)不能保证。但你得到的想法...:)

Disclaimer: I just hacked this together, quality (or even compilation) not guaranteed. But you get the idea... :)

这篇关于使用访客模式时的匿名或真实类定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆